Esempio n. 1
0
def test_debiasing_warm_start():
    clf = PrimalSVC(kernel="rbf", gamma=0.1, penalty="l1", max_iter=10)
    clf.C = 0.5
    clf.fit(bin_dense, bin_target)
    assert_equal(clf.n_support_vectors(), 160)
    assert_almost_equal(clf.score(bin_dense, bin_target), 0.845)

    clf = PrimalSVC(kernel="rbf", gamma=0.1, penalty="l1", max_iter=10)
    clf.C = 0.500001
    clf.fit(bin_dense, bin_target)
    assert_equal(clf.n_support_vectors(), 191)
    assert_almost_equal(clf.score(bin_dense, bin_target), 0.97)

    clf = PrimalSVC(kernel="rbf", gamma=0.1, penalty="l1", max_iter=10,
                    warm_start=True)
    clf.C = 0.5
    clf.fit(bin_dense, bin_target)
    assert_equal(clf.n_support_vectors(), 160)
    assert_almost_equal(clf.score(bin_dense, bin_target), 0.845)

    clf = PrimalSVC(kernel="rbf", gamma=0.1, penalty="l1", max_iter=10,
                    warm_start=True)
    clf.C = 0.500001
    clf.fit(bin_dense, bin_target)
    assert_equal(clf.n_support_vectors(), 191)
    assert_almost_equal(clf.score(bin_dense, bin_target), 0.97)
Esempio n. 2
0
def test_warm_start_l2r_rbf():
    clf = PrimalSVC(warm_start=True, kernel="rbf", gamma=0.1,
                    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)
Esempio n. 3
0
def test_components():
    clf = PrimalSVC(random_state=0, penalty="l1", kernel="rbf",
                    gamma=0.1, C=0.5)
    clf.fit(bin_dense, bin_target)
    acc = clf.score(bin_dense, bin_target)

    clf = PrimalSVC(random_state=0, penalty="l2", kernel="rbf",
                    gamma=0.1, C=0.5, components=clf.support_vectors_)
    clf.fit(bin_dense, bin_target)
    assert_equal(clf.n_support_vectors(), 160)
    acc2 = clf.score(bin_dense, bin_target)
    assert_equal(acc, acc2)
Esempio n. 4
0
def test_empty_model():
    clf = PrimalSVC(kernel="rbf", gamma=0.1, C=0.1, penalty="l1")
    clf.fit(bin_dense, bin_target)
    assert_equal(clf.n_support_vectors(), 0)
    acc = clf.score(bin_dense, bin_target)
    assert_equal(acc, 0.5)

    clf = PrimalSVC(kernel="rbf", gamma=0.1, C=0.1, penalty="l1l2")
    clf.fit(bin_dense, bin_target)
    assert_equal(clf.n_support_vectors(), 0)
    acc = clf.score(bin_dense, bin_target)
    assert_equal(acc, 0.5)
Esempio n. 5
0
def test_fit_rbf_binary_l2r_kernelized_selection():
    clf = PrimalSVC(C=1.0, random_state=0, penalty="l2", loss="squared_hinge",
                    max_iter=1, kernel="rbf", kernel_regularizer=True,
                    selection="loss")
    clf.fit(bin_dense, bin_target)
    acc = clf.score(bin_dense, bin_target)
    assert_almost_equal(acc, 1.0)
Esempio n. 6
0
def test_fit_rbf_binary_l2r():
    clf = PrimalSVC(C=0.5, kernel="rbf", gamma=0.1, random_state=0, penalty="l2")
    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)
    assert_equal(n_nz, 200) # dense solution...
Esempio n. 7
0
def test_fit_rbf_binary_l2r_kernelized_upper_bound():
    clf = PrimalSVC(C=1.0, random_state=0, penalty="l2", loss="squared_hinge",
                    max_iter=20, kernel="rbf", kernel_regularizer=True,
                    selection="loss", termination="n_components", n_components=30)
    clf.fit(bin_dense, bin_target)
    acc = clf.score(bin_dense, bin_target)
    assert_almost_equal(acc, 0.80)
    assert_equal(clf.n_support_vectors(), 30)

    clf = PrimalSVC(C=1.0, random_state=0, penalty="l2l2", loss="squared_hinge",
                    max_iter=20, kernel="rbf", kernel_regularizer=True,
                    selection="loss", termination="n_components", n_components=30,
                    warm_debiasing=True)
    clf.fit(bin_dense, bin_target)
    acc = clf.score(bin_dense, bin_target)
    assert_almost_equal(acc, 0.80)
    assert_equal(clf.n_support_vectors(), 30)
Esempio n. 8
0
def test_fit_rbf_binary_l1r_selection():
    for selection in ("loss", "active"):
        clf = PrimalSVC(C=0.5, kernel="rbf", gamma=0.1, random_state=0,
                        penalty="l1", selection=selection)
        clf.fit(bin_dense, bin_target)
        acc = clf.score(bin_dense, bin_target)
        assert_true(acc >= 0.74)
        n_nz = np.sum(clf.coef_ != 0)
        assert_true(n_nz <= 86)
Esempio n. 9
0
def test_debiasing():
    clf = PrimalSVC(kernel="rbf", gamma=0.1, penalty="l1l2", C=0.5, Cd=1.0,
                    max_iter=10)
    clf.fit(bin_dense, bin_target)
    assert_equal(clf.n_support_vectors(), 160)
    assert_almost_equal(clf.score(bin_dense, bin_target), 0.845)
    pred = clf.decision_function(bin_dense)

    clf = PrimalSVC(kernel="rbf", gamma=0.1, penalty="l1", C=0.5)
    clf.fit(bin_dense, bin_target)
    assert_equal(clf.n_support_vectors(), 160)
    K = pairwise_kernels(bin_dense, clf.support_vectors_, metric="rbf", gamma=0.1)
    clf = PrimalLinearSVC(max_iter=10, C=1.0)
    clf.fit(K, bin_target)
    assert_almost_equal(clf.score(K, bin_target), 0.845)
    pred2 = clf.decision_function(K)

    assert_array_almost_equal(pred, pred2)
Esempio n. 10
0
def test_fit_rbf_binary_l2r_correctness_kernelized():
    for loss in ("squared_hinge", "modified_huber", "log"):
        clf = PrimalSVC(C=1.0, random_state=0, penalty="l2", loss=loss,
                        max_iter=1, kernel="rbf", kernel_regularizer=True)
        clf.fit(bin_dense, bin_target)
        acc = clf.score(bin_dense, bin_target)
        assert_almost_equal(acc, 1.0)

        clf2 = PrimalKernelSVC(C=1.0, random_state=0, loss=loss,
                               max_iter=1, kernel="rbf",
                               kernel_regularizer=True)
        clf2.fit(bin_dense, bin_target)
        assert_array_almost_equal(clf.coef_, clf2.coef_, decimal=5)
Esempio n. 11
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)
Esempio n. 12
0
    def fit(self):
        print "fit the model"
        train = np.array(self.model.data)
        X = train[:, 0:2]
        y = train[:, 2]

        C = float(self.complexity.get())
        gamma = float(self.gamma.get())
        coef0 = float(self.coef0.get())
        degree = int(self.degree.get())
        kernel_map = {0: "linear", 1: "rbf", 2: "poly"}
        clf = PrimalSVC(kernel=kernel_map[self.kernel.get()], C=C,
                        gamma=gamma, coef0=coef0, degree=degree,
                        max_iter=50, penalty="l1")
        clf.fit(X, y)
        if hasattr(clf, 'score'):
            print "Accuracy:", clf.score(X, y) * 100
        X1, X2, Z = self.decision_surface(clf)
        self.model.clf = clf
        self.model.set_surface((X1, X2, Z))
        self.model.surface_type = self.surface_type.get()
        self.fitted = True
        self.model.changed("surface")
Esempio n. 13
0
def test_fit_rbf_binary_l2r_modified_huber():
    clf = PrimalSVC(C=1.0, random_state=0, penalty="l2",
                    kernel="rbf", loss="modified_huber")
    clf.fit(bin_dense, bin_target)
    acc = clf.score(bin_dense, bin_target)
    assert_almost_equal(acc, 1.0)
Esempio n. 14
0
def test_fit_rbf_binary_l2r_log():
    clf = PrimalSVC(C=1.0, random_state=0, penalty="l2", loss="log",
                          max_iter=5, kernel="rbf")
    clf.fit(bin_dense, bin_target)
    acc = clf.score(bin_dense, bin_target)
    assert_almost_equal(acc, 1.0)