def test_warm_start_l2r(): clf = PrimalLinearSVC(warm_start=True, random_state=0, penalty="l2") clf.C = 0.1 clf.fit(bin_dense, bin_target) assert_almost_equal(clf.score(bin_dense, bin_target), 1.0) clf.C = 0.2 clf.fit(bin_dense, bin_target) assert_almost_equal(clf.score(bin_dense, bin_target), 1.0)
def test_fit_rbf_binary_l1r(): clf = PrimalSVC(C=0.5, kernel="rbf", gamma=0.1, random_state=0, penalty="l1") clf.fit(bin_dense, bin_target) acc = clf.score(bin_dense, bin_target) assert_almost_equal(acc, 0.845) n_nz = np.sum(clf.coef_ != 0) assert_equal(n_nz, 160) K = pairwise_kernels(bin_dense, metric="rbf", gamma=0.1) clf2 = PrimalLinearSVC(C=0.5, random_state=0, penalty="l1") clf2.fit(K, bin_target) acc = clf2.score(K, bin_target) assert_almost_equal(acc, 0.845) n_nz = np.sum(clf2.coef_ != 0) assert_equal(n_nz, 160)
def test_fit_linear_binary_l2r(): clf = PrimalLinearSVC(C=1.0, random_state=0, penalty="l2") clf.fit(bin_dense, bin_target) acc = clf.score(bin_dense, bin_target) assert_almost_equal(acc, 1.0) K = pairwise_kernels(bin_dense, metric="rbf", gamma=0.1) clf2 = PrimalLinearSVC(C=0.5, random_state=0, penalty="l2") clf2.fit(K, bin_target) acc = clf2.score(K, bin_target) assert_almost_equal(acc, 1.0) n_nz = np.sum(clf2.coef_ != 0) assert_equal(n_nz, 200)
def test_fit_linear_binary_l1r(): clf = PrimalLinearSVC(C=1.0, random_state=0, penalty="l1") clf.fit(bin_dense, bin_target) acc = clf.score(bin_dense, bin_target) assert_almost_equal(acc, 1.0) n_nz = np.sum(clf.coef_ != 0) clf = PrimalLinearSVC(C=0.1, random_state=0, penalty="l1") clf.fit(bin_dense, bin_target) acc = clf.score(bin_dense, bin_target) assert_almost_equal(acc, 0.97) n_nz2 = np.sum(clf.coef_ != 0) assert_true(n_nz > n_nz2)
def test_warm_start_l1r(): clf = PrimalLinearSVC(warm_start=True, random_state=0, penalty="l1") clf.C = 0.1 clf.fit(bin_dense, bin_target) n_nz = np.sum(clf.coef_ != 0) clf.C = 0.2 clf.fit(bin_dense, bin_target) n_nz2 = np.sum(clf.coef_ != 0) assert_true(n_nz < n_nz2)
def test_lower_bound_binary(): Cmin = C_lower_bound(bin_dense, bin_target) clf = PrimalLinearSVC(C=Cmin, random_state=0, penalty="l1") clf.fit(bin_dense, bin_target) n_nz = np.sum(clf.coef_ != 0) assert_equal(0, n_nz) clf = PrimalLinearSVC(C=Cmin * 2, random_state=0, penalty="l1") clf.fit(bin_dense, bin_target) n_nz = np.sum(clf.coef_ != 0) assert_not_equal(0, n_nz)
def test_fit_squared_loss(): clf = PrimalLinearSVC(C=1.0, random_state=0, penalty="l2", loss="squared", max_iter=100) clf.fit(bin_dense, bin_target) assert_almost_equal(clf.score(bin_dense, bin_target), 0.99) y = bin_target.copy() y[y == 0] = -1 assert_array_almost_equal(1 - y * np.dot(bin_dense, clf.coef_.ravel()), clf.errors_.ravel()) K = pairwise_kernels(bin_dense, metric="rbf", gamma=0.1) for kernel_regularizer in (True, False): clf = PrimalSVC(C=1.0, random_state=0, penalty="l2", kernel="rbf", gamma=0.1, selection="loss", loss="squared", max_iter=100, kernel_regularizer=kernel_regularizer) clf.fit(bin_dense, bin_target) assert_almost_equal(clf.score(bin_dense, bin_target), 1.0) assert_array_almost_equal(1 - y * np.dot(K, clf.coef_.ravel()), clf.errors_.ravel())
def test_fit_linear_binary_l2r_modified_huber(): clf = PrimalLinearSVC(C=1.0, random_state=0, penalty="l2", loss="modified_huber") clf.fit(bin_dense, bin_target) acc = clf.score(bin_dense, bin_target) assert_almost_equal(acc, 1.0)
def test_fit_linear_binary_l2r_log(): clf = PrimalLinearSVC(C=1.0, random_state=0, penalty="l2", loss="log", max_iter=5) clf.fit(bin_dense, bin_target) acc = clf.score(bin_dense, bin_target) assert_almost_equal(acc, 1.0)