Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make final calculation of coefficients optional. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions fracridge/fracridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
__all__ = ["fracridge", "vec_len", "FracRidge"]


def fracridge(X, y, fracs=None, tol=1e-6):
def fracridge(X, y, fracs=None, tol=1e-6, return_coef=True):
"""
Approximates alpha parameters to match desired fractions of OLS length.

Expand All @@ -45,6 +45,9 @@ def fracridge(X, y, fracs=None, tol=1e-6):
OLS solution. If 1d array, the shape is (f,).
Default: np.arange(.1, 1.1, .1)

return_coef : bool, optional
Whether to calculate the resulting coefficients, or return only
the best alphas.

Returns
-------
Expand Down Expand Up @@ -116,10 +119,13 @@ def fracridge(X, y, fracs=None, tol=1e-6):
sc = seltsq / (seltsq + targetalphas[np.newaxis].T)
coef[..., ii] = (sc * ols_coef[..., ii]).T

coef = np.reshape(v_t.T @ coef.reshape((first_dim, ff * bb)),
if return_coef:
coef = np.reshape(v_t.T @ coef.reshape((first_dim, ff * bb)),
(pp, ff, bb))

return coef.squeeze(), alphas
return coef.squeeze(), alphas
else:
return alphas


class FracRidge(BaseEstimator, MultiOutputMixin):
Expand Down
2 changes: 2 additions & 0 deletions fracridge/tests/test_fracridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def test_fracridge_ols(nn, pp, bb):
coef = coef[:, -1, ...]
assert np.allclose(coef, coef_ols, atol=10e-3)
assert np.all(np.diff(alpha, axis=0) <= 0)
alpha = fracridge(X, y, fracs=fracs, return_coef=False)
assert np.all(np.diff(alpha, axis=0) <= 0)


@pytest.mark.parametrize("frac", [0.1, 0.23, 1])
Expand Down