def bench_scikit(X, Y, T): """ bench with scikit-learn bindings on libsvm """ import scikits.learn from scikits.learn.svm import SVC gc.collect() # start time tstart = datetime.now() clf = SVC(kernel='linear'); clf.fit(X, Y); Z = clf.predict(T) delta = (datetime.now() - tstart) # stop time scikit_results.append(delta.seconds + delta.microseconds/mu_second)
def test_SVMModelField(): X = [[0 ,0],[1, 1]] y = [0, 1] svm = SVM() clf = SVC() clf.fit(X,y) a1 = clf.predict([[2.,2.]]) #print clf #print a1 svm.classifier = clf svm.save(safe=True) s = SVM.objects.first() #print s.classifier a2 = s.classifier.predict([[2., 2.]]) #print a2 assert a1 == a2
# project the input data on the eigenfaces orthonormal basis X_train_pca = pca.transform(X_train) X_test_pca = pca.transform(X_test) ################################################################################ # Train a SVM classification model print "Fitting the classifier to the training set" clf = SVC(C=100).fit(X_train_pca, y_train, class_weight="auto") ################################################################################ # Quantitative evaluation of the model quality on the test set y_pred = clf.predict(X_test_pca) print classification_report(y_test, y_pred, labels=selected_target, class_names=category_names[selected_target]) print confusion_matrix(y_test, y_pred, labels=selected_target) ################################################################################ # Qualitative evaluation of the predictions using matplotlib n_row = 3 n_col = 4 pl.figure(figsize=(2*n_col, 2.3*n_row)) pl.subplots_adjust(bottom=0, left=.01, right=.99, top=.95, hspace=.15) for i in range(n_row * n_col):
import numpy as np X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]]) y = np.array([1, 1, 2, 2]) from scikits.learn.svm import SVC clf = SVC() clf.fit(X, y) print clf.predict([[-0.8, -1]])