示例#1
0
def test_poisson_with_sparsity():
    """Tests the Poisson fitter with no intercept."""
    n_features = 3
    n_samples = 10000

    # create data
    X, y, beta, _ = make_poisson_regression(n_samples=n_samples,
                                            n_features=n_features,
                                            n_informative=n_features,
                                            beta=np.array([0., 1.0, 1.5]),
                                            random_state=2332)

    # lbfgs
    poisson = Poisson(alpha=0.1, l1_ratio=1., fit_intercept=False,
                      solver='lbfgs', max_iter=5000)
    poisson.fit(X, y)

    assert_equal(np.abs(poisson.coef_[0]), 0)

    # coordinate descent
    poisson = Poisson(alpha=0.1, l1_ratio=1., fit_intercept=False, solver='cd',
                      max_iter=5000)
    poisson.fit(X, y)

    assert_equal(np.abs(poisson.coef_[0]), 0.)
示例#2
0
def test_poisson_with_intercept():
    """Tests the Poisson fitter with no intercept."""
    n_features = 3
    n_samples = 10000

    # create data
    X, y, beta, intercept = make_poisson_regression(
        n_samples=n_samples,
        n_features=n_features,
        n_informative=n_features,
        beta=np.array([0.5, 1.0, 1.5]),
        include_intercept=True,
        random_state=2332)

    # lbfgs
    poisson = Poisson(alpha=0., l1_ratio=0., fit_intercept=True,
                      solver='lbfgs', max_iter=5000)
    poisson.fit(X, y)

    assert_allclose(poisson.coef_, beta, rtol=0.5)
    assert_allclose(poisson.intercept_, intercept, rtol=0.5)

    # coordinate descent
    poisson = Poisson(alpha=0., l1_ratio=0., fit_intercept=True, solver='cd',
                      max_iter=5000)
    poisson.fit(X, y)

    assert_allclose(poisson.coef_, beta, rtol=0.5)
    assert_allclose(poisson.intercept_, intercept, rtol=0.5)
示例#3
0
def test_poisson_warm_start():
    """Tests if the warm start initializes coefficients correctly."""
    n_features = 3
    n_samples = 10000

    # create data
    X, y, beta, _ = make_poisson_regression(n_samples=n_samples,
                                            n_features=n_features,
                                            n_informative=n_features,
                                            beta=np.array([0.5, 1.0, 1.5]),
                                            random_state=2332)

    # lbfgs
    poisson = Poisson(alpha=0., l1_ratio=0., fit_intercept=False,
                      solver='lbfgs', max_iter=5000, warm_start=True)
    poisson.fit(X, y)
    first_coef = poisson.coef_

    poisson.coef_ = np.zeros(n_features)
    poisson.fit(X, y)
    second_coef = poisson.coef_

    assert_allclose(first_coef, second_coef, rtol=0.1)

    # cd
    poisson = Poisson(alpha=0., l1_ratio=0., fit_intercept=False,
                      solver='cd', max_iter=5000, warm_start=True)
    poisson.fit(X, y)
    first_coef = poisson.coef_

    poisson.coef_ = np.zeros(n_features)
    poisson.fit(X, y)
    second_coef = poisson.coef_

    assert_allclose(first_coef, second_coef, rtol=0.1)
示例#4
0
def test_poisson_no_intercept():
    """Tests the Poisson fitter with no intercept."""
    n_features = 3
    n_samples = 10000

    # create data
    X, y, beta, _ = make_poisson_regression(n_samples=n_samples,
                                            n_features=n_features,
                                            n_informative=n_features,
                                            beta=np.array([0.5, 1.0, 1.5]),
                                            random_state=2332)

    # lbfgs
    poisson = Poisson(alpha=0., l1_ratio=0., fit_intercept=False,
                      solver='lbfgs', max_iter=5000)
    poisson.fit(X, y)
    assert_allclose(poisson.coef_, beta, rtol=0.5)

    # coordinate descent
    poisson = Poisson(alpha=0., l1_ratio=0., fit_intercept=False, solver='cd',
                      max_iter=5000)
    poisson.fit(X, y)
    assert_allclose(poisson.coef_, beta, rtol=0.5)

    # broken solver
    poisson = Poisson(alpha=0., l1_ratio=0., fit_intercept=False, solver='ruff',
                      max_iter=5000)
    assert_raises(ValueError, poisson.fit, X, y)
示例#5
0
def test_poisson_reg_params():
    """Test whether the upper bound on the regularization parameters correctly
    zero out the coefficients."""
    n_features = 5
    n_samples = 1000

    X, y, beta, intercept = make_poisson_regression(
        n_samples=n_samples,
        n_features=n_features,
        n_informative=n_features,
        random_state=2332)

    alpha_sets = [np.array([0.5]), np.array([1.0])]

    for alpha_set in alpha_sets:
        uoi_poisson = UoI_Poisson(alphas=alpha_set)
        reg_params = uoi_poisson.get_reg_params(X, y)
        alpha = reg_params[0]['alpha']
        l1_ratio = reg_params[0]['l1_ratio']

        # check that coefficients get set to zero
        poisson = Poisson(alpha=1.01 * alpha,
                          l1_ratio=l1_ratio,
                          standardize=False,
                          fit_intercept=True)
        poisson.fit(X, y)
        assert_equal(poisson.coef_, 0.)

        # check that coefficients below the bound are not set to zero
        poisson = Poisson(alpha=0.99 * alpha,
                          l1_ratio=l1_ratio,
                          standardize=False,
                          fit_intercept=True)
        poisson.fit(X, y)
        assert np.count_nonzero(poisson.coef_) > 0
def test_poisson():
    """Test basic functionality of UoI_Lasso and that it finds right model"""
    n_features = 20
    n_inf = 10
    X, y, w, b = make_poisson_regression(n_samples=200,
                                         n_features=n_features,
                                         n_informative=n_inf,
                                         random_state=10)
    poisson = UoI_Poisson(comm=MPI.COMM_WORLD)
    poisson.fit(X, y)
    assert (np.sign(abs(w)) == np.sign(abs(poisson.coef_))).mean() >= .6
示例#7
0
def test_UoI_Poisson():
    """Tests the UoI Poisson fitter with lbfgs solver."""
    n_features = 3
    n_samples = 5000

    # create data
    X, y, beta, _ = make_poisson_regression(n_samples=n_samples,
                                            n_features=n_features,
                                            n_informative=n_features,
                                            beta=np.array([0., 1.0, 1.5]),
                                            random_state=2332)

    # lbfgs
    poisson = UoI_Poisson(n_lambdas=48, n_boots_sel=30, n_boots_est=30,
                          alphas=np.array([1.0]), warm_start=False)
    poisson.fit(X, y)

    assert_allclose(poisson.coef_, beta, atol=0.5)
示例#8
0
def test_poisson_standardize():
    """Tests the Poisson fitter `standardize=True`."""
    n_features = 3
    n_samples = 200

    # create data
    X, y, beta, intercept = make_poisson_regression(
        n_samples=n_samples,
        n_features=n_features,
        n_informative=n_features,
        beta=np.array([0.5, 1.0, 1.5]),
        include_intercept=True,
        random_state=2332)

    # lbfgs
    poisson = Poisson(alpha=0., l1_ratio=0., fit_intercept=True,
                      solver='lbfgs', standardize=True)
    poisson.fit(X, y)