def catClassify(botData, kernelType, nTopic): X = botData[:, :nTopic] y = botData[:, nTopic] # Run classifier # classifier = svm.SVC(kernel='linear', probability=True) classifier = svm.NuSVC(probability=True) #cross-validation cv = StratifiedKFold(y, k=nFold) #select classifier #classifier = svm.SVC(kernel=kernelType, probability=True) metricstemp = np.zeros((nFold, nMetrics), np.float) for i, (train, test) in enumerate(cv): probas_ = classifier.fit(X[train], y[train]).predict_proba(X[test]) #fpr, tpr, thresholds = roc_curve(y[test], probas_[:,1]) #@UnusedVariable #roc_auc = auc(fpr, tpr) precision, recall, thresholds = precision_recall_curve( y[test], probas_[:, 1]) #@UnusedVariable pr_auc = auc(recall, precision) metricstemp[i] = [pr_auc] return [np.mean(metricstemp), np.std(metricstemp)]
def test_bad_input(): """ Test that it gives proper exception on deficient input """ # impossible value of C assert_raises(ValueError, svm.SVC(C=-1).fit, X, Y) # impossible value of nu clf = svm.NuSVC(nu=0.0) assert_raises(ValueError, clf.fit, X, Y) Y2 = Y[:-1] # wrong dimensions for labels assert_raises(ValueError, clf.fit, X, Y2) # Test with arrays that are non-contiguous. Xf = np.asfortranarray(X) clf = svm.SVC() clf.fit(Xf, Y) assert_array_equal(clf.predict(T), true_result) # error for precomputed kernelsx clf = svm.SVC(kernel='precomputed') assert_raises(ValueError, clf.fit, X, Y) Xt = np.array(X).T clf = svm.SVC(kernel='precomputed') clf.fit(np.dot(X, Xt), Y) assert_raises(ValueError, clf.predict, X) clf = svm.SVC() clf.fit(X, Y) assert_raises(ValueError, clf.predict, Xt)
def test_error(): """ Test that it gives proper exception on deficient input """ # impossible value of C assert_raises(ValueError, svm.SVC(C=-1).fit, X, Y) # impossible value of nu clf = svm.NuSVC(nu=0.0) assert_raises(ValueError, clf.fit, X, Y) Y2 = Y[:-1] # wrong dimensions for labels assert_raises(ValueError, clf.fit, X, Y2) assert_raises(ValueError, svm.SVC, X, Y2) # Test with arrays that are non-contiguous. Xf = np.asfortranarray(X) clf = svm.SVC() clf.fit(Xf, Y) assert_array_equal(clf.predict(T), true_result)
def test_probability(): """ Predict probabilities using SVC This uses cross validation, so we use a slightly bigger testing set. """ for clf in (svm.SVC(probability=True), svm.NuSVC(probability=True), svm.sparse.SVC(probability=True), svm.sparse.NuSVC(probability=True)): clf.fit(iris.data, iris.target) prob_predict = clf.predict_proba(iris.data) assert_array_almost_equal(np.sum(prob_predict, 1), np.ones(iris.data.shape[0])) assert np.mean( np.argmax(prob_predict, 1) == clf.predict(iris.data)) > 0.9 assert_almost_equal(clf.predict_proba(iris.data), np.exp(clf.predict_log_proba(iris.data)), 8)
def test_bad_input(): """ Test that it gives proper exception on deficient input """ # impossible value of C assert_raises(ValueError, svm.SVC(C=-1).fit, X, Y) # impossible value of nu clf = svm.NuSVC(nu=0.0) assert_raises(ValueError, clf.fit, X, Y) Y2 = Y[:-1] # wrong dimensions for labels assert_raises(ValueError, clf.fit, X, Y2) # Test with arrays that are non-contiguous. for clf in (svm.SVC(), svm.LinearSVC(), svm.sparse.SVC(), svm.sparse.LinearSVC()): Xf = np.asfortranarray(X) assert Xf.flags['C_CONTIGUOUS'] == False yf = np.ascontiguousarray(np.tile(Y, (2, 1)).T) yf = yf[:, -1] assert yf.flags['F_CONTIGUOUS'] == False assert yf.flags['C_CONTIGUOUS'] == False clf.fit(Xf, yf) assert_array_equal(clf.predict(T), true_result) # error for precomputed kernelsx clf = svm.SVC(kernel='precomputed') assert_raises(ValueError, clf.fit, X, Y) Xt = np.array(X).T clf = svm.SVC(kernel='precomputed') clf.fit(np.dot(X, Xt), Y) assert_raises(ValueError, clf.predict, X) clf = svm.SVC() clf.fit(X, Y) assert_raises(ValueError, clf.predict, Xt)
import pylab as pl from scikits.learn import svm, datasets # import some data to play with iris = datasets.load_iris() X = iris.data[:, :2] # we only take the first two features. We could # avoid this ugly slicing by using a two-dim dataset Y = iris.target h=.02 # step size in the mesh # we create an instance of SVM and fit out data. We do not scale our # data since we want to plot the support vectors svc = svm.SVC(kernel='linear').fit(X, Y) rbf_svc = svm.SVC(kernel='poly').fit(X, Y) nu_svc = svm.NuSVC(kernel='linear').fit(X,Y) lin_svc = svm.LinearSVC().fit(X, Y) # create a mesh to plot in x_min, x_max = X[:,0].min()-1, X[:,0].max()+1 y_min, y_max = X[:,1].min()-1, X[:,1].max()+1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) # title for the plots titles = ['SVC with linear kernel', 'SVC with polynomial (degree 3) kernel', 'NuSVC with linear kernel', 'LinearSVC (linear kernel)']
with RBF kernel. The target to predict is a XOR of the inputs. """ print __doc__ import numpy as np import pylab as pl from scikits.learn import svm xx, yy = np.meshgrid(np.linspace(-5, 5, 500), np.linspace(-5, 5, 500)) np.random.seed(0) X = np.random.randn(300, 2) Y = np.logical_xor(X[:,0]>0, X[:,1]>0) # fit the model clf = svm.NuSVC() clf.fit(X, Y) # plot the line, the points, and the nearest vectors to the plane Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) pl.set_cmap(pl.cm.Paired) pl.pcolormesh(xx, yy, Z) pl.scatter(X[:,0], X[:,1], c=Y) pl.axis('tight') pl.show()