예제 #1
0
def test_elnet_n_lambda():
    rng = np.random.default_rng(SEED)
    error = rng.normal(loc=0, scale=1, size=100)
    X = rng.normal(loc=5, scale=2, size=(100, 4))
    true_betas = np.array([1, -2, 0.5, 1])
    y = X.dot(true_betas) + error
    elnet = Elnet()
    elnet.fit(X, y)
예제 #2
0
def test_predict_custom_lambda():
    rng = np.random.default_rng(SEED)
    error = rng.normal(loc=0, scale=1, size=100)
    X = rng.normal(loc=5, scale=2, size=(100, 4))
    true_betas = np.array([1, -2, 0.5, 1])
    y = X.dot(true_betas) + error
    m = Elnet(n_splits=3)
    m.fit(X, y)
    m.predict(X, lamb=1)
예제 #3
0
def test_predict_without_cv():
    rng = np.random.default_rng(SEED)
    error = rng.normal(loc=0, scale=1, size=100)
    X = rng.normal(loc=5, scale=2, size=(100, 4))
    true_betas = np.array([1, -2, 0.5, 1])
    y = X.dot(true_betas) + error
    m = Elnet(n_splits=1)
    m.fit(X, y)
    with pytest.raises(Exception):
        m.predict(X)
예제 #4
0
def test_predict_multiple_lambdas():
    rng = np.random.default_rng(SEED)
    error = rng.normal(loc=0, scale=1, size=100)
    X = rng.normal(loc=5, scale=2, size=(100, 4))
    true_betas = np.array([1, -2, 0.5, 1])
    y = X.dot(true_betas) + error
    m = Elnet(n_splits=3)
    m.fit(X, y)
    preds = m.predict(X, lamb=[0.5, 1, 1.5])
    assert preds.shape == (100, 3)
예제 #5
0
def test_fit_cv_glmnet_comparison():
    rng = np.random.default_rng(SEED)
    error = rng.normal(loc=0, scale=1, size=100)
    X = rng.normal(loc=5, scale=2, size=(100, 4))
    true_betas = np.array([1, -2, 0.5, 1])
    y = X.dot(true_betas) + error
    m = Elnet(n_splits=3, random_state=182, scoring="r2")
    m.fit(X, y)
    m2 = ElasticNet(n_splits=3, random_state=182)
    m2.fit(X, y)
    np.testing.assert_almost_equal(m.lambda_max_, m2.lambda_max_)
    np.testing.assert_almost_equal(m.lambda_1se_, m2.lambda_best_[0])
예제 #6
0
def test_fit_cv():
    rng = np.random.default_rng(SEED)
    error = rng.normal(loc=0, scale=1, size=100)
    X = rng.normal(loc=5, scale=2, size=(100, 4))
    true_betas = np.array([1, -2, 0.5, 1])
    y = X.dot(true_betas) + error
    m = Elnet(n_splits=3)
    m.fit(X, y)
    assert hasattr(m, "lambda_1se_")
    assert hasattr(m, "lambda_max_")
    assert hasattr(m, "cv_mean")
    assert hasattr(m, "cv_se")
예제 #7
0
def test_elnet_glmnet(alpha):
    # compare to original glmnet Fortran algorithm
    rng = np.random.default_rng(SEED)
    error = rng.normal(loc=0, scale=1, size=100)
    X = rng.normal(loc=5, scale=2, size=(100, 4))
    true_betas = np.array([1, -2, 0.5, 1])
    y = X.dot(true_betas) + error
    # glmnet
    m = ElasticNet(alpha=alpha)
    m.fit(X, y)
    # own implementation
    m2 = Elnet(alpha=alpha)
    m2.fit(X, y)
    # same lambda sequence
    np.testing.assert_almost_equal(m.lambda_path_, m2.lambda_path_)
    # same feature coefficients
    # decimal = 4: almost equal to the 4th decimal
    np.testing.assert_almost_equal(m.coef_path_, m2.coef_path_, decimal=4)
    # same intercept path
    np.testing.assert_almost_equal(m.intercept_path_, m2.intercept_path_, decimal=4)