def train(xs, ys, update_fn=None, typecode=None):
 if (len(xs) != len(ys)):
  raise ValueError('xs and ys should be the same length.')
 classes = set(ys)
 if (classes != set([0, 1])):
  raise ValueError("Classes should be 0's and 1's")
 if (typecode is None):
  typecode = 'd'
 (N, ndims) = (len(xs), (len(xs[0]) + 1))
 if ((N == 0) or (ndims == 1)):
  raise ValueError('No observations or observation of 0 dimension.')
 X = numpy.ones((N, ndims), typecode)
 X[:, 1:] = xs
 Xt = numpy.transpose(X)
 y = numpy.asarray(ys, typecode)
 beta = numpy.zeros(ndims, typecode)
 MAX_ITERATIONS = 500
 CONVERGE_THRESHOLD = 0.01
 stepsize = 1.0
 i = 0
 old_beta = old_llik = None
 while (i < MAX_ITERATIONS):
  ebetaX = numpy.exp(numpy.dot(beta, Xt))
  p = (ebetaX / (1 + ebetaX))
  logp = ((y * numpy.log(p)) + ((1 - y) * numpy.log((1 - p))))
  llik = sum(logp)
  if (update_fn is not None):
   update_fn(iter, llik)
  if (old_llik is not None):
   if (llik < old_llik):
    stepsize /= 2.0
    beta = old_beta
   if (numpy.fabs((llik - old_llik)) <= CONVERGE_THRESHOLD):
    break
  (old_llik, old_beta) = (llik, beta)
  i += 1
  W = (numpy.identity(N) * p)
  Xtyp = numpy.dot(Xt, (y - p))
  XtWX = numpy.dot(numpy.dot(Xt, W), X)
  delta = numpy.linalg.solve(XtWX, Xtyp)
  if (numpy.fabs((stepsize - 1.0)) > 0.001):
   delta *= stepsize
  beta += delta
 else:
  raise RuntimeError("Didn't converge.")
 lr = LogisticRegression()
 lr.beta = [float(x) for x in beta]
 return lr

def train ( xs , ys , update_fn = None , typecode = None ) : if ( len ( xs ) != len ( ys ) ) : raise ValueError ( 'xs and ys should be the same length.' ) classes = set ( ys ) if ( classes != set ( [ 0 , 1 ] ) ) : raise ValueError ( "Classes should be 0's and 1's" ) if ( typecode is None ) : typecode = 'd' ( N , ndims ) = ( len ( xs ) , ( len ( xs [ 0 ] ) + 1 ) ) if ( ( N == 0 ) or ( ndims == 1 ) ) : raise ValueError ( 'No observations or observation of 0 dimension.' ) X = numpy . ones ( ( N , ndims ) , typecode ) X [ : , 1 : ] = xs Xt = numpy . transpose ( X ) y = numpy . asarray ( ys , typecode ) beta = numpy . zeros ( ndims , typecode ) MAX_ITERATIONS = 500 CONVERGE_THRESHOLD = 0.01 stepsize = 1.0 i = 0 old_beta = old_llik = None while ( i < MAX_ITERATIONS ) : ebetaX = numpy . exp ( numpy . dot ( beta , Xt ) ) p = ( ebetaX / ( 1 + ebetaX ) ) logp = ( ( y * numpy . log ( p ) ) + ( ( 1 - y ) * numpy . log ( ( 1 - p ) ) ) ) llik = sum ( logp ) if ( update_fn is not None ) : update_fn ( iter , llik ) if ( old_llik is not None ) : if ( llik < old_llik ) : stepsize /= 2.0 beta = old_beta if ( numpy . fabs ( ( llik - old_llik ) ) <= CONVERGE_THRESHOLD ) : break ( old_llik , old_beta ) = ( llik , beta ) i += 1 W = ( numpy . identity ( N ) * p ) Xtyp = numpy . dot ( Xt , ( y - p ) ) XtWX = numpy . dot ( numpy . dot ( Xt , W ) , X ) delta = numpy . linalg . solve ( XtWX , Xtyp ) if ( numpy . fabs ( ( stepsize - 1.0 ) ) > 0.001 ) : delta *= stepsize beta += delta else : raise RuntimeError ( "Didn't converge." ) lr = LogisticRegression ( ) lr . beta = [ float ( x ) for x in beta ] return lr
train a model .