def test_sample_weight(queue): X = np.array([[-2, 0], [-1, -1], [0, -2], [0, 2], [1, 1], [2, 2]]) y = np.array([1, 1, 1, 2, 2, 2]) clf = NuSVC(kernel='linear') clf.fit(X, y, sample_weight=[1] * 6, queue=queue) assert_array_almost_equal(clf.intercept_, [0.0])
def test_class_weight(queue): X = np.array([[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]]) y = np.array([1, 1, 1, 2, 2, 2]) clf = NuSVC(class_weight={1: 0.1}) clf.fit(X, y, queue=queue) assert_array_almost_equal(clf.predict(X, queue=queue), [2] * 6)
def test_decision_function_shape(queue): X, y = make_blobs(n_samples=80, centers=5, random_state=0) X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0) # check shape of ovo_decition_function=True clf = NuSVC(kernel='linear', decision_function_shape='ovo').fit(X_train, y_train, queue=queue) dec = clf.decision_function(X_train, queue=queue) assert dec.shape == (len(X_train), 10)
def test_decision_function(queue): X = [[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]] Y = [1, 1, 1, 2, 2, 2] clf = NuSVC(kernel='rbf', gamma=1, decision_function_shape='ovo') clf.fit(X, Y, queue=queue) rbfs = rbf_kernel(X, clf.support_vectors_, gamma=clf.gamma) dec = np.dot(rbfs, clf.dual_coef_.T) + clf.intercept_ assert_array_almost_equal(dec.ravel(), clf.decision_function(X, queue=queue))
def _test_libsvm_parameters(queue, array_constr, dtype): X = array_constr([[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]], dtype=dtype) y = array_constr([1, 1, 1, 2, 2, 2], dtype=dtype) clf = NuSVC(kernel='linear').fit(X, y, queue=queue) assert_array_almost_equal( clf.dual_coef_, [[-0.04761905, -0.0952381, 0.0952381, 0.04761905]]) assert_array_equal(clf.support_, [0, 1, 3, 4]) assert_array_equal(clf.support_vectors_, X[clf.support_]) assert_array_equal(clf.intercept_, [0.]) assert_array_equal(clf.predict(X, queue=queue), y)
def test_pickle(queue): iris = datasets.load_iris() clf = NuSVC(kernel='linear').fit(iris.data, iris.target, queue=queue) expected = clf.decision_function(iris.data, queue=queue) import pickle dump = pickle.dumps(clf) clf2 = pickle.loads(dump) assert type(clf2) == clf.__class__ result = clf2.decision_function(iris.data, queue=queue) assert_array_equal(expected, result)
def _test_cancer_poly_compare_with_sklearn(queue, params): cancer = datasets.load_breast_cancer() clf = NuSVC(kernel='poly', **params) clf.fit(cancer.data, cancer.target, queue=queue) result = clf.score(cancer.data, cancer.target, queue=queue) clf = SklearnNuSVC(kernel='poly', **params) clf.fit(cancer.data, cancer.target) expected = clf.score(cancer.data, cancer.target) assert result > 0.5 assert abs(result - expected) < 1e-4
def _test_cancer_linear_compare_with_sklearn(queue, nu): cancer = datasets.load_breast_cancer() clf = NuSVC(kernel='linear', nu=nu) clf.fit(cancer.data, cancer.target, queue=queue) result = clf.score(cancer.data, cancer.target, queue=queue) clf = SklearnNuSVC(kernel='linear', nu=nu) clf.fit(cancer.data, cancer.target) expected = clf.score(cancer.data, cancer.target) assert result > 0.5 assert abs(result - expected) < 1e-3
def _test_cancer_rbf_compare_with_sklearn(queue, nu, gamma): cancer = datasets.load_breast_cancer() clf = NuSVC(kernel='rbf', gamma=gamma, nu=nu) clf.fit(cancer.data, cancer.target, queue=queue) result = clf.score(cancer.data, cancer.target, queue=queue) clf = SklearnNuSVC(kernel='rbf', gamma=gamma, nu=nu) clf.fit(cancer.data, cancer.target) expected = clf.score(cancer.data, cancer.target) assert result > 0.4 assert abs(result - expected) < 1e-4
def test_iris(queue): iris = datasets.load_iris() clf = NuSVC(kernel='linear').fit(iris.data, iris.target, queue=queue) assert clf.score(iris.data, iris.target, queue=queue) > 0.9 assert_array_equal(clf.classes_, np.sort(clf.classes_))