def bench_scikit(X, Y): """ 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='rbf') clf.fit(X, Y).predict(X) delta = (datetime.now() - tstart) # stop time scikit_results.append(delta.seconds + delta.microseconds / mu_second)
def bench_scikit(X, Y): """ 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='rbf') clf.fit(X, Y).predict(X) 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
<<<<<<< HEAD rfe = RFE(estimator = SVC(kernel="linear",C=1), n_features = 10, percentage = 0.1) anova_filter = UnivariateFilter(SelectKBest(k=10), f_classif) clf = SVC(kernel="linear",C=1) y_pred_rfe = [] y_pred_univ = [] y_true = [] for train, test in StratifiedKFold(y, 2): Xtrain, ytrain, Xtest, ytest = X[train], y[train], X[test], y[test] ### Fit and predict rfe support = rfe.fit(X[train], y[train]).support_ y_pred_rfe.append(clf.fit(X[train,support],y[train]).predict( X[test,support])) ### Fit and predict univariate feature selection xr = anova_filter.fit(Xtrain, ytrain).transform(Xtrain) y_pred_univ.append(clf.fit(Xtrain[:,anova_filter.support_],ytrain).predict( Xtest[:,anova_filter.support_])) y_true.append(ytest) y_pred_univ = np.concatenate(y_pred_univ) y_true = np.concatenate(y_true) classif_rate_univ = np.mean(y_pred_univ == y_true) * 100 print "Classification rate univariate: %f" % classif_rate_univ ======= svc = SVC(kernel="linear", C=1) rfe = RFE(estimator=svc, n_features=30, percentage=0.1) rfe.fit(X, y)
# along with this program. If not, see <http://www.gnu.org/licenses/>. from __future__ import division import os import logging import pickle import numpy as np from scikits.learn.svm import SVC from string import punctuation from operator import itemgetter logging.basicConfig(level=logging.DEBUG) lab_train, vec_train, lab_test, vec_test = [ pickle.load(open(file)) for file in [ 'labels_training.pik', 'vectors_training.pik', 'labels_test.pik', 'vectors_test.pik' ] ] logging.info("Data loaded") cat_train = list(set(lab_train)) cat_test = list(set(lab_test)) assert cat_test == cat_train lab_train = [cat_train.index(l) for l in lab_train] lab_test = [cat_test.index(l) for l in lab_test] clf = SVC(kernel='rbf') clf.fit(vec_train, lab_train) pickle.dump(clf, open('classifier.pik', 'wb'))
from scikits.learn.svm import SVC from string import punctuation from operator import itemgetter logging.basicConfig(level=logging.DEBUG) lab_train, vec_train , lab_test, vec_test = [pickle.load(open(file)) for file in ['labels_training.pik', 'vectors_training.pik', 'labels_test.pik', 'vectors_test.pik']] logging.info("Data loaded") cat_train = list(set(lab_train)) cat_test = list(set(lab_test)) assert cat_test == cat_train lab_train = [cat_train.index(l) for l in lab_train] lab_test = [cat_test.index(l) for l in lab_test] clf = SVC(kernel='rbf') clf.fit(vec_train, lab_train) pickle.dump(clf,open('classifier.pik','wb'))
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]])