def test_FracRidgeRegressor_predict(nn, pp, bb, fit_intercept, jit): X, y, coef_ols, pred_ols = make_data(nn, pp, bb, fit_intercept) fracs = np.arange(.1, 1.1, .1) FR = FracRidgeRegressor(fracs=fracs, fit_intercept=fit_intercept, jit=jit) FR.fit(X, y) pred_fr = FR.predict(X) assert np.allclose(pred_fr[:, -1, ...], pred_ols, atol=10e-3)
def test_fracridge_singleton_target(nn, pp, fit_intercept, fracs, y_is_a_single_column_matrix): # Sometimes we want to have just one target, and this should work for either a vector y or single-column matrix y, # See: https://github.com/nrdg/fracridge/issues/39 bb = 1 if fracs is None: fracs = np.arange(.1, 1.1, .1) X, y, _, pred_ols = make_data(nn, pp, bb, fit_intercept=fit_intercept) if y_is_a_single_column_matrix: # expand y from a vector to a one-column matrix y = np.expand_dims(y, axis=-1) pred_ols = np.expand_dims(pred_ols, -1) # do the same for the ols prediction # else, y remains a vector FR = FracRidgeRegressor(fracs=fracs, fit_intercept=fit_intercept) FR.fit(X, y) pred_fr = FR.predict(X) if hasattr( fracs, "__len__" ): # if fracs is a a vector, extract the 1.0 fraction coefficient column (which should be similar to the OLS solution) pred_fr = pred_fr[:, np.asarray(fracs) == 1.0, ...].squeeze(1) assert np.allclose(pred_fr, pred_ols, atol=10e-3) assert np.array_equal(y.shape, pred_fr.shape) # predictions should be shaped like y
def test_v_fracs(nn, pp, bb, frac): X, y, coef_ols, _ = make_data(nn, pp, bb) FR = FracRidgeRegressor(fracs=frac) FR.fit(X, y) assert np.all( np.abs(frac - vec_len(FR.coef_, axis=0) / vec_len(coef_ols, axis=0)) < 0.01)
def test_FracRidge_singleton_frac(): X = np.array([[1.64644051], [2.1455681]]) y = np.array([1., 2.]) fracs = 0.1 FR = FracRidgeRegressor(fracs=fracs) FR.fit(X, y) pred_fr = FR.predict(X) assert pred_fr.shape == y.shape
def test_fracridge_single_regressor(nn, bb, fit_intercept): # Sometimes we want to have just one regressor # See: https://github.com/nrdg/fracridge/issues/24 pp = 1 X, y, _, pred_ols = make_data(nn, pp, bb, fit_intercept=fit_intercept) fracs = np.arange(.1, 1.1, .1) FR = FracRidgeRegressor(fracs=fracs, fit_intercept=fit_intercept) FR.fit(X, y) pred_fr = FR.predict(X).squeeze() assert np.allclose(pred_fr[:, -1, ...], pred_ols, atol=10e-3)
def test_FracRidgeRegressorCV(nn, pp, bb, fit_intercept, jit): X, y, _, _ = make_data(nn, pp, bb, fit_intercept) fracs = np.arange(.1, 1.1, .1) FRCV = FracRidgeRegressorCV(fit_intercept=fit_intercept, jit=jit) FRCV.fit(X, y, frac_grid=fracs) FR = FracRidgeRegressor(fracs=FRCV.best_frac_, fit_intercept=fit_intercept) FR.fit(X, y) assert np.allclose(FR.coef_, FRCV.coef_, atol=10e-3) RR = Ridge(alpha=FRCV.alpha_, fit_intercept=fit_intercept, solver='svd') RR.fit(X, y) # The coefficients in the sklearn object are transposed relative to # our conventions: assert np.allclose(RR.coef_.T, FRCV.coef_, atol=10e-3)
def test_v_ols(nn, pp, bb, fit_intercept): X, y, coef_ols, _ = make_data(nn, pp, bb) fracs = np.arange(.1, 1.1, .1) FR = FracRidgeRegressor(fracs=fracs, fit_intercept=fit_intercept) FR.fit(X, y) assert np.allclose(FR.coef_[:, -1, ...], coef_ols, atol=10e-3)
def test_FracRidge_estimator(): check_estimator(FracRidgeRegressor()) check_estimator(FracRidgeRegressorCV())
def __init__(self, fracs=None, fit_intercept=False, normalize=False, copy_X=True, tol=1e-10, jit=True): FracRidgeRegressor.__init__( self, fracs=fracs, fit_intercept=False, normalize=False, copy_X=True, tol=tol, jit=True)
for aa in range(len(rr_alphas)): RR = Ridge(alpha=rr_alphas[aa], fit_intercept=True) RR.fit(X, y) rr_coefs[:, aa] = RR.coef_ rr_pred[:, aa] = cross_val_predict(RR, X, y) ########################################################################## # In contrast, FRR takes as inputs fractions, rather than arbitrarily-chosen # values of alpha. The alphas that are generated are selected to produce # solutions whos fractional L2-norm relative to the L2-norm of the # unregularized solution are these values. Here too, cross-validated # predictions are generated: fracs = np.linspace(0, 1, n_alphas) FR = FracRidgeRegressor(fracs=fracs, fit_intercept=True) FR.fit(X, y) fr_pred = cross_val_predict(FR, X, y) ########################################################################## # We plot the results. First, the FRR coefficients as a function of requested # fractions and then the RR coefficient as a function of the requested # log-spaced alpha: fig, ax = plt.subplots(1, 2) ax[0].plot(fracs, FR.coef_.T) ylims = ax[0].get_ylim() ax[0].vlines(fracs, ylims[0], ylims[1], linewidth=0.5, color='gray') ax[0].set_ylim(*ylims) ax[1].plot(np.log(rr_alphas[::-1]), rr_coefs.T) ylims = ax[1].get_ylim()
import numpy as np from fracridge import (fracridge, vec_len, FracRidgeRegressor, FracRidgeRegressorCV) from sklearn.linear_model import Ridge import pytest from sklearn.utils.estimator_checks import parametrize_with_checks @parametrize_with_checks([FracRidgeRegressor(), FracRidgeRegressorCV()]) def test_sklearn_compatible_estimator(estimator, check): check(estimator) def run_fracridge(X, y, fracs, jit): fracridge(X, y, fracs=fracs, jit=jit) @pytest.mark.parametrize("nn, pp", [(1000, 10), (10, 100), (284, 50)]) @pytest.mark.parametrize("bb", [(1), (2), (1000)]) @pytest.mark.parametrize("jit", [True, False]) def test_benchmark_fracridge(nn, pp, bb, jit, benchmark): X, y, _, _ = make_data(nn, pp, bb) fracs = np.arange(.1, 1.1, .1) benchmark(run_fracridge, X, y, fracs, jit) def make_data(nn, pp, bb, fit_intercept=False): np.random.seed(1) X = np.random.randn(nn, pp) y = np.random.randn(nn, bb).squeeze() if fit_intercept: