Beispiel #1
0
def test_log_reg_vs_graph_net_two_classes_iris(C=.01,
                                               tol=1e-10,
                                               zero_thr=1e-4):
    # Test for one of the extreme cases of Graph-Net: That is, with
    # l1_ratio = 1 (pure Lasso), we compare Graph-Net's coefficients'
    # performance with the coefficients obtained from Scikit-Learn's
    # LogisticRegression, with L1 penalty, in a 2 classes classification task
    iris = load_iris()
    X, y = iris.data, iris.target
    y = 2 * (y > 0) - 1
    X_, mask = to_niimgs(X, (2, 2, 2))
    tvl1 = SpaceNetClassifier(mask=mask,
                              alphas=1. / C / X.shape[0],
                              l1_ratios=1.,
                              tol=tol,
                              verbose=0,
                              max_iter=1000,
                              penalty="tv-l1",
                              standardize=False,
                              screening_percentile=100.).fit(X_, y)
    sklogreg = LogisticRegression(penalty="l1",
                                  fit_intercept=True,
                                  tol=tol,
                                  C=C).fit(X, y)

    # compare supports
    np.testing.assert_array_equal((np.abs(tvl1.coef_) < zero_thr),
                                  (np.abs(sklogreg.coef_) < zero_thr))

    # compare predictions
    np.testing.assert_array_equal(tvl1.predict(X_), sklogreg.predict(X))
Beispiel #2
0
def test_log_reg_vs_graph_net_two_classes_iris(C=.01, tol=1e-10,
                                               zero_thr=1e-4):
    # Test for one of the extreme cases of Graph-Net: That is, with
    # l1_ratio = 1 (pure Lasso), we compare Graph-Net's coefficients'
    # performance with the coefficients obtained from Scikit-Learn's
    # LogisticRegression, with L1 penalty, in a 2 classes classification task
    iris = load_iris()
    X, y = iris.data, iris.target
    y = 2 * (y > 0) - 1
    X_, mask = to_niimgs(X, (2, 2, 2))
    tvl1 = SpaceNetClassifier(
        mask=mask, alphas=1. / C / X.shape[0], l1_ratios=1., tol=tol,
        verbose=0, max_iter=1000, penalty="tv-l1", standardize=False,
        screening_percentile=100.).fit(X_, y)
    sklogreg = LogisticRegression(penalty="l1",
                                  fit_intercept=True,
                                  solver='liblinear',
                                  tol=tol,
                                  C=C,
                                  ).fit(X, y)

    # compare supports
    np.testing.assert_array_equal((np.abs(tvl1.coef_) < zero_thr),
                                  (np.abs(sklogreg.coef_) < zero_thr))

    # compare predictions
    np.testing.assert_array_equal(tvl1.predict(X_), sklogreg.predict(X))
Beispiel #3
0
def test_graph_net_classifier_score():
    iris = load_iris()
    X, y = iris.data, iris.target
    y = 2 * (y > 0) - 1
    X_, mask = to_niimgs(X, (2, 2, 2))
    gnc = SpaceNetClassifier(mask=mask, alphas=1. / .01 / X.shape[0],
                             l1_ratios=1., tol=1e-10,
                             standardize=False, verbose=0,
                             screening_percentile=100.).fit(X_, y)
    accuracy = gnc.score(X_, y)
    assert_equal(accuracy, accuracy_score(y, gnc.predict(X_)))