Beispiel #1
0
def test_sag_adaptive():
    """Check that the adaptive step size strategy yields the same
    solution as the non-adaptive"""
    np.random.seed(0)
    X = sparse.rand(100, 10, density=.5, random_state=0).tocsr()
    y = np.random.randint(0, high=2, size=100)
    for alpha in np.logspace(-3, 1, 5):
        clf_adaptive = SAGClassifier(eta='line-search',
                                     random_state=0,
                                     alpha=alpha)
        clf_adaptive.fit(X, y)
        clf = SAGClassifier(eta='auto', random_state=0, alpha=alpha)
        clf.fit(X, y)
        np.testing.assert_almost_equal(clf_adaptive.score(X, y),
                                       clf.score(X, y), 1)

        clf_adaptive = SAGAClassifier(eta='line-search',
                                      loss='log',
                                      random_state=0,
                                      alpha=alpha,
                                      max_iter=20)
        clf_adaptive.fit(X, y)
        assert np.isnan(clf_adaptive.coef_.sum()) == False
        clf = SAGAClassifier(eta='auto',
                             loss='log',
                             random_state=0,
                             alpha=alpha,
                             max_iter=20)
        clf.fit(X, y)
        np.testing.assert_almost_equal(clf_adaptive.score(X, y),
                                       clf.score(X, y), 1)
Beispiel #2
0
def test_sag_sparse():
    # FIX for https://github.com/mblondel/lightning/issues/33
    # check that SAG has the same results with dense
    # and sparse data
    X = sparse.rand(100, 50, density=.5, random_state=0)
    y = np.random.randint(0, high=2, size=100)
    for alpha in np.logspace(-3, 3, 10):
        clf_sparse = SAGClassifier(eta=1.,
                                   max_iter=1,
                                   random_state=0,
                                   alpha=alpha)
        clf_sparse.fit(X, y)
        clf_dense = SAGClassifier(eta=1.,
                                  max_iter=1,
                                  random_state=0,
                                  alpha=alpha)
        clf_dense.fit(X.toarray(), y)
        assert clf_sparse.score(X, y) == clf_dense.score(X, y)

        clf_sparse = SAGAClassifier(eta=1.,
                                    max_iter=1,
                                    random_state=0,
                                    alpha=alpha)
        clf_sparse.fit(X, y)
        clf_dense = SAGAClassifier(eta=1.,
                                   max_iter=1,
                                   random_state=0,
                                   alpha=alpha)
        clf_dense.fit(X.toarray(), y)
        assert clf_sparse.score(X, y) == clf_dense.score(X, y)
Beispiel #3
0
def test_sag_sparse():
    # FIX for https://github.com/mblondel/lightning/issues/33
    # check that SAG has the same results with dense
    # and sparse data
    X = sparse.rand(100, 50, density=.5, random_state=0)
    y = np.random.randint(0, high=2, size=100)
    for alpha in np.logspace(-3, 3, 10):
        clf_sparse = SAGClassifier(max_iter=1, random_state=0, alpha=alpha)
        clf_sparse.fit(X, y)
        clf_dense = SAGClassifier(max_iter=1, random_state=0, alpha=alpha)
        clf_dense.fit(X.toarray(), y)
        assert_equal(clf_sparse.score(X, y), clf_dense.score(X, y))
Beispiel #4
0
def test_sag_score():
    X, y = make_classification(1000, random_state=0)

    pysag = PySAGClassifier(eta=1e-3, alpha=0.0, beta=0.0, max_iter=10, random_state=0)
    sag = SAGClassifier(eta=1e-3, alpha=0.0, beta=0.0, max_iter=10, random_state=0)

    pysag.fit(X, y)
    sag.fit(X, y)
    assert_equal(pysag.score(X, y), sag.score(X, y))
Beispiel #5
0
def test_sag_score():
    X, y = make_classification(1000, random_state=0)

    pysag = PySAGClassifier(eta=1e-3, alpha=0.0, beta=0.0, max_iter=10,
                            random_state=0)
    sag = SAGClassifier(eta=1e-3, alpha=0.0, beta=0.0, max_iter=10,
                        random_state=0)

    pysag.fit(X, y)
    sag.fit(X, y)
    assert_equal(pysag.score(X, y), sag.score(X, y))
Beispiel #6
0
def test_sag_adaptive():
    """Check that the adaptive step size strategy yields the same
    solution as the non-adaptive"""
    np.random.seed(0)
    X = sparse.rand(100, 10, density=.5, random_state=0).tocsr()
    y = np.random.randint(0, high=2, size=100)
    for alpha in np.logspace(-3, 1, 5):
        clf_adaptive = SAGClassifier(
            eta='line-search', random_state=0, alpha=alpha)
        clf_adaptive.fit(X, y)
        clf = SAGClassifier(
            eta='auto', random_state=0, alpha=alpha)
        clf.fit(X, y)
        assert_almost_equal(clf_adaptive.score(X, y), clf.score(X, y), 1)

        clf_adaptive = SAGAClassifier(
            eta='line-search', loss='log', random_state=0, alpha=alpha, max_iter=20)
        clf_adaptive.fit(X, y)
        assert np.isnan(clf_adaptive.coef_.sum()) == False
        clf = SAGAClassifier(
            eta='auto', loss='log', random_state=0, alpha=alpha, max_iter=20)
        clf.fit(X, y)
        assert_almost_equal(clf_adaptive.score(X, y), clf.score(X, y), 1)
Beispiel #7
0
def test_sag():
    clf = SAGClassifier(eta=1e-3, max_iter=20, verbose=0, random_state=0)
    clf.fit(X_bin, y_bin)
    assert_equal(clf.score(X_bin, y_bin), 1.0)
Beispiel #8
0
def test_sag():
    clf = SAGClassifier(eta=1e-3, max_iter=20, verbose=0, random_state=0)
    clf.fit(X_bin, y_bin)
    assert_equal(clf.score(X_bin, y_bin), 1.0)