Пример #1
0
def test_fit_rbf_binary_early_stopping():
    clf = DualSVC(loss="l1", kernel="rbf", gamma=0.5, random_state=0,
                  shrinking=True, selection="loss",
                  termination="n_sv", sv_upper_bound=30)
    clf.fit(bin_dense, bin_target)
    y_pred = clf.predict(bin_dense)
    assert_equal(clf.dual_coef_.shape[1], 30)
def fit_dual_svc(X_train, y_train, C, kernel, gamma=0.1, degree=4, coef0=1):
    print "Training dual SVC, C = ", C
    start = time.time()
    clf = DualSVC(C=C, kernel=kernel, degree=degree, coef0=coef0, gamma=gamma,
                  max_iter=100, loss="l1", tol=1e-4, verbose=1)
    clf.fit(X_train, y_train)
    return clf, time.time() - start
Пример #3
0
def test_fit_rbf_binary():
    for shrinking in (True, False):
        for selection in ("loss", "permute", "active"):
            for loss in ("l1", "l2"):
                clf = DualSVC(loss=loss, kernel="rbf", gamma=0.1, random_state=0,
                              shrinking=shrinking, selection=selection)
                clf.fit(bin_dense, bin_target)
                y_pred = clf.predict(bin_dense)
                assert_equal(np.mean(y_pred == bin_target), 1.0)
Пример #4
0
def test_fit_linear_binary():
    for loss in ("l1", "l2"):
        clf = DualLinearSVC(loss=loss, random_state=0, max_iter=100)
        clf.fit(bin_dense, bin_target)
        y_pred = clf.decision_function(bin_dense)

        clf2 = DualSVC(loss=loss, random_state=0, max_iter=100)
        clf2.fit(bin_dense, bin_target)
        y_pred2 = clf2.decision_function(bin_dense)

        assert_array_almost_equal(y_pred, y_pred2)
Пример #5
0
def test_warm_start():
    clf = DualSVC(warm_start=True, loss="l1", kernel="linear", random_state=0,
                  max_iter=100)
    clf2 = DualLinearSVC(warm_start=True, loss="l1", random_state=0,
                         max_iter=100)

    for C in (0.1, 0.2):
        clf.C = C
        clf2.C = C

        clf.fit(bin_dense, bin_target)
        y_pred = clf.decision_function(bin_dense)
        clf2.fit(bin_dense, bin_target)
        y_pred2 = clf2.decision_function(bin_dense)

        assert_array_almost_equal(y_pred, y_pred2)
Пример #6
0
def test_precomputed_kernel():
    clf = DualSVC(loss="l1", kernel="linear", random_state=0)
    clf.fit(bin_dense, bin_target)
    y_pred = clf.decision_function(bin_dense)

    clf = DualSVC(loss="l1", kernel="precomputed", random_state=0)
    K = np.dot(bin_dense, bin_dense.T)
    clf.fit(K, bin_target)
    y_pred2 = clf.decision_function(K)

    assert_array_almost_equal(y_pred, y_pred2)
Пример #7
0
def test_fit_rbf_multi():
    clf = DualSVC(kernel="rbf", gamma=0.1, random_state=0)
    clf.fit(mult_dense, mult_target)
    y_pred = clf.predict(mult_dense)
    acc = np.mean(y_pred == mult_target)
    assert_almost_equal(acc, 1.0)