def eval_kfold(A, B, train, labels, shuffle=True, k=10, seed=1234, use_feats=False):
 labels = np.array(labels)
 if use_feats:
  features = np.c_[(np.abs((A - B)), (A * B), feats(train[0], train[1]))]
 else:
  features = np.c_[(np.abs((A - B)), (A * B))]
 scan = [(2 ** t) for t in range(0, 9, 1)]
 npts = len(features)
 kf = KFold(npts, n_folds=k, shuffle=shuffle, random_state=seed)
 scores = []
 for s in scan:
  scanscores = []
  for (train, test) in kf:
   X_train = features[train]
   y_train = labels[train]
   X_test = features[test]
   y_test = labels[test]
   clf = LogisticRegression(C=s)
   clf.fit(X_train, y_train)
   yhat = clf.predict(X_test)
   fscore = f1(y_test, yhat)
   scanscores.append(fscore)
   print (s, fscore)
  scores.append(np.mean(scanscores))
  print scores
 s_ind = np.argmax(scores)
 s = scan[s_ind]
 print scores
 print s
 return s

def eval_kfold ( A , B , train , labels , shuffle = True , k = 10 , seed = 1234 , use_feats = False ) : labels = np . array ( labels ) if use_feats : features = np . c_ [ ( np . abs ( ( A - B ) ) , ( A * B ) , feats ( train [ 0 ] , train [ 1 ] ) ) ] else : features = np . c_ [ ( np . abs ( ( A - B ) ) , ( A * B ) ) ] scan = [ ( 2 ** t ) for t in range ( 0 , 9 , 1 ) ] npts = len ( features ) kf = KFold ( npts , n_folds = k , shuffle = shuffle , random_state = seed ) scores = [ ] for s in scan : scanscores = [ ] for ( train , test ) in kf : X_train = features [ train ] y_train = labels [ train ] X_test = features [ test ] y_test = labels [ test ] clf = LogisticRegression ( C = s ) clf . fit ( X_train , y_train ) yhat = clf . predict ( X_test ) fscore = f1 ( y_test , yhat ) scanscores . append ( fscore ) print ( s , fscore ) scores . append ( np . mean ( scanscores ) ) print scores s_ind = np . argmax ( scores ) s = scan [ s_ind ] print scores print s return s
perform k-fold cross validation .