def nfoldCV_SVM(Features, Labels, KF, C, i, Scores, kernel): nIterate = 1 score = np.zeros([10]) jobs = [] op.colPrint("C: %s" % C, col="c") for train, test in KF: jobs.append( threading.Thread(target=one_trial_SVM, args=(nIterate, Features, Labels, train, test, C, score, kernel)) ) nIterate = nIterate + 1 for j in jobs: j.start() for j in jobs: j.join() Scores[i, :] = score op.colPrint("C: %s; done." % C, col="y")
import sys from sklearn import svm from sklearn.externals import joblib import option_10foldcv as op if __name__ == '__main__': if len(sys.argv) < 2: modelName = "clf.model" else: modelName = sys.argv[1] # read feature vector and corresponding labels ====================== op.colPrint("Reading features & labels ================", col='c') Features = op.readFeatures() Labels = op.readLabels() if Features.shape[0] != Labels.shape[0]: sys.exit("The number of samples and labels are different.") nSamples = Features.shape[0] op.colPrint("Reading features & labels; done. Number of samples: %s" % nSamples, col='y') # create svm instance and training clf = svm.SVC(kernel='linear', C=10**1, probability=True).fit(Features, Labels) # export classifier print "Save classifier as", modelName joblib.dump(clf, modelName)
# main ================================================================== if __name__ == "__main__": pid = os.getpid() print "pid:", pid if len(sys.argv) < 2: kernel = "linear" else: kernel = sys.argv[1] print "kernel:", kernel # read feature vector and corresponding labels ====================== op.colPrint("Reading features & labels ================", col="c") Features = op.readFeatures() Labels = op.readLabels() if Features.shape[0] != Labels.shape[0]: sys.exit("The number of samples and labels are different.") nSamples = Features.shape[0] op.colPrint("Reading features & labels; done. Number of samples: %s" % nSamples, col="y") # split samples (N = 10) ============================================ kf = KFold(nSamples, n_folds=10, shuffle=True, random_state=None) # Parameter of SVM =========================== C = np.logspace(-5, 5, num=11) # C = np.logspace(-5, 4, num=10) Scores = np.zeros([len(C), 10])