Example #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))
Example #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))
Example #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_)))
Example #4
0
def test_space_net_classifier_subclass():
    for penalty, alpha, l1_ratio, verbose in itertools.product(
            ["graph-net", "tv-l1"], [.4, .01], [.5, 1.], [True, False]):
        cvobj = SpaceNetClassifier(
            mask="dummy", penalty=penalty, alphas=alpha, l1_ratios=l1_ratio,
            verbose=verbose)
        assert_equal(cvobj.alphas, alpha)
        assert_equal(cvobj.l1_ratios, l1_ratio)
def test_graph_net_and_tvl1_same_for_pure_l1_logistic(max_iter=20,
                                                      decimal=2):
    ###############################################################
    # graph_net_solver and tvl1_solver should give same results
    # when l1_ratio = 1.
    ###############################################################

    iris = load_iris()
    X, y = iris.data, iris.target
    y = y > 0.
    alpha = 1. / X.shape[0]
    X_, mask_ = to_niimgs(X, (2, 2, 2))
    mask = mask_.get_data().astype(np.bool).ravel()

    # results should be exactly the same for pure lasso
    a = _graph_net_logistic(X, y, alpha, 1., mask=mask,
                            max_iter=max_iter)[0]
    b = tvl1_solver(X, y, alpha, 1., loss="logistic", mask=mask,
                    max_iter=max_iter)[0]
    for standardize in [True, False]:
        sl = SpaceNetClassifier(
            alphas=alpha,
            l1_ratios=1.,
            max_iter=max_iter,
            mask=mask_,
            penalty="graph-net",
            standardize=standardize).fit(
            X_,
            y)
        tvl1 = SpaceNetClassifier(
            alphas=alpha,
            l1_ratios=1.,
            max_iter=max_iter,
            mask=mask_,
            penalty="tv-l1",
            standardize=standardize).fit(
            X_,
            y)

    # should be exactly the same (except for numerical errors)
    np.testing.assert_array_almost_equal(a, b, decimal=decimal)
    np.testing.assert_array_almost_equal(sl.coef_[0], tvl1.coef_[0],
                                         decimal=decimal)