Exemplo n.º 1
0
def Lasso(X, r, lambda_, r_var=None):
    """
    Function for finding regression coefficients implementing lasso regression
    from sklearn.
    Note that in order for this function to work in harmony with the other
    functions of the project, an empty array is returned in place of the
    coefficient variances. The sklearn function does not provide any
    variances.
    Arguments:
        X (array): design matrix
        r (array): response vector
        lambda_ (float): tuning parameter
        r_var (float, optional): variance of response is calculated if none is
                                 provided, defaults to None
    Returns:
        beta (array): vector of coefficients
        beta_var (array): zeroes array of same shape as beta
    """
    if r_var is None:
        r_var = np.var(r)

    model = sklearn_Lasso(alpha=lambda_,
                          fit_intercept=False,
                          max_iter=int(1e6))
    model.fit(X, r)

    beta = model.coef_

    # sklearn Lasso does not provide any var
    beta_var = np.zeros(beta.shape)

    return beta, beta_var
Exemplo n.º 2
0
def test_dropin_lasso(sparse_X):
    """Test that our Lasso class behaves as sklearn's Lasso."""
    X, y, _, _ = build_dataset(n_samples=20, n_features=30, sparse_X=sparse_X)

    alpha_max = np.linalg.norm(X.T.dot(y), ord=np.inf) / X.shape[0]
    alpha = alpha_max / 2.
    clf = Lasso(alpha=alpha)
    clf.fit(X, y)

    clf2 = sklearn_Lasso(alpha=alpha)
    clf2.fit(X, y)
    np.testing.assert_allclose(clf.coef_, clf2.coef_, rtol=1e-5)

    check_estimator(Lasso)
Exemplo n.º 3
0
def test_dropin_lasso(sparse_X, fit_intercept):
    """Test that our Lasso class behaves as sklearn's Lasso."""
    X, y, _, _ = build_dataset(n_samples=20, n_features=30, sparse_X=sparse_X)

    alpha_max = np.linalg.norm(X.T.dot(y), ord=np.inf) / X.shape[0]
    alpha = alpha_max / 2.
    params = dict(alpha=alpha,
                  fit_intercept=fit_intercept,
                  tol=1e-10,
                  normalize=True)
    clf = Lasso(**params)
    clf.fit(X, y)

    clf2 = sklearn_Lasso(**params)
    clf2.fit(X, y)
    np.testing.assert_allclose(clf.coef_, clf2.coef_, rtol=1e-5)
    if fit_intercept:
        np.testing.assert_allclose(clf.intercept_, clf2.intercept_)

    check_estimator(Lasso)
Exemplo n.º 4
0
def test_Lasso(sparse_X, fit_intercept, positive):
    """Test that our Lasso class behaves as sklearn's Lasso."""
    X, y = build_dataset(n_samples=20, n_features=30, sparse_X=sparse_X)
    if not positive:
        alpha_max = norm(X.T.dot(y), ord=np.inf) / X.shape[0]
    else:
        alpha_max = X.T.dot(y).max() / X.shape[0]

    alpha = alpha_max / 2.
    params = dict(alpha=alpha,
                  fit_intercept=fit_intercept,
                  tol=1e-10,
                  positive=positive)
    clf = Lasso(**params)
    clf.fit(X, y)

    clf2 = sklearn_Lasso(**params)
    clf2.fit(X, y)
    assert_allclose(clf.coef_, clf2.coef_, rtol=1e-5)
    if fit_intercept:
        assert_allclose(clf.intercept_, clf2.intercept_)