|
1 | 1 | """ |
2 | 2 |
|
3 | | -=============================== |
4 | | -Example using scikit learn data |
5 | | -=============================== |
| 3 | +======================================== |
| 4 | +Example using scikit learn diabetes data |
| 5 | +======================================== |
6 | 6 |
|
7 | | -We're going to compare the performance of the fracrdige algorithm with the |
8 | | -performance of a standard approach to selection of regularization parameters: |
9 | | -log-spaced selection of alphas between very minimal regularization and |
| 7 | +This example demonstrates some of the properties of the FRR approach and |
| 8 | +compares it to the use of standard ridge regression (RR) on the diabetes |
| 9 | +dataset that is included in sckit-learn. |
| 10 | +
|
| 11 | +In standard ridge regression, it is common to select alpha by testing a |
| 12 | +range of log-spaced values between very minimal regularization and |
10 | 13 | very strong regularization. |
11 | 14 |
|
12 | 15 | """ |
13 | 16 |
|
| 17 | +########################################################################## |
| 18 | +# Imports: |
| 19 | +# |
| 20 | + |
14 | 21 | import numpy as np |
15 | 22 | import matplotlib.pyplot as plt |
| 23 | +########################################################################## |
| 24 | +# This is the fracridge sklearn-style estimator: |
| 25 | +# |
16 | 26 | from fracridge import FracRidge |
17 | 27 |
|
18 | 28 | from sklearn import datasets |
19 | 29 | from sklearn.linear_model import Ridge |
20 | 30 | from sklearn.model_selection import cross_val_predict |
21 | 31 | from sklearn.metrics import r2_score |
22 | 32 |
|
| 33 | +########################################################################## |
| 34 | +# Get example data from scikit learn: |
| 35 | +# |
| 36 | + |
23 | 37 | X, y = datasets.load_diabetes(return_X_y=True) |
24 | 38 |
|
| 39 | +########################################################################## |
| 40 | +# Values of alpha for the standard approach are set to be 20 values |
| 41 | +# That are log-spaced from a very small value to a very large value: |
| 42 | +# |
25 | 43 | n_alphas = 20 |
26 | | -rr_alphas = alphas = np.logspace(-10, 10, n_alphas) |
| 44 | +rr_alphas = np.logspace(-10, 10, n_alphas) |
27 | 45 | rr_coefs = [] |
28 | 46 | rr_coefs = np.zeros((X.shape[-1], n_alphas)) |
29 | 47 | rr_pred = np.zeros((y.shape[-1], n_alphas)) |
| 48 | + |
| 49 | +########################################################################## |
| 50 | +# We calculate the fit and cross-validated prediction for each value of |
| 51 | +# alpha: |
| 52 | + |
30 | 53 | for aa in range(len(rr_alphas)): |
31 | 54 | RR = Ridge(alpha=rr_alphas[aa], fit_intercept=True) |
32 | 55 | RR.fit(X, y) |
33 | 56 | rr_coefs[:, aa] = RR.coef_ |
34 | 57 | rr_pred[:, aa] = cross_val_predict(RR, X, y) |
35 | 58 |
|
| 59 | +########################################################################## |
| 60 | +# In contrast, FRR takes as inputs fractions, rather than arbitrarily-chosen |
| 61 | +# values of alpha. The alphas that are generated are selected to produce |
| 62 | +# solutions whos fractional L2-norm relative to the L2-norm of the |
| 63 | +# unregularized solution are these values. Here too, cross-validated |
| 64 | +# predictions are generated: |
| 65 | + |
36 | 66 | fracs = np.linspace(0, 1, n_alphas) |
37 | 67 | FR = FracRidge(fracs=fracs, fit_intercept=True) |
38 | 68 | FR.fit(X, y) |
39 | 69 | fr_pred = cross_val_predict(FR, X, y) |
40 | 70 |
|
| 71 | +########################################################################## |
| 72 | +# We plot the results. First, the FRR coefficients as a function of requested |
| 73 | +# fractions: |
| 74 | + |
41 | 75 | fig, ax = plt.subplots(1, 2) |
42 | 76 | ax[0].plot(fracs, FR.coef_.T) |
43 | 77 | ylims = ax[0].get_ylim() |
44 | 78 | ax[0].vlines(fracs, ylims[0], ylims[1], linewidth=0.5, color='gray') |
45 | 79 | ax[0].set_ylim(*ylims) |
46 | 80 |
|
| 81 | +########################################################################## |
| 82 | +# Next, the RR as a function of the requested log-spaced alpha: |
47 | 83 | ax[1].plot(np.log(rr_alphas[::-1]), rr_coefs.T) |
48 | 84 | ylims = ax[1].get_ylim() |
49 | 85 | ax[1].vlines(np.log(rr_alphas[::-1]), ylims[0], ylims[1], linewidth=0.5, |
50 | 86 | color='gray') |
51 | 87 | ax[1].set_ylim(*ylims) |
52 | 88 |
|
| 89 | +########################################################################## |
| 90 | +# In a second plot, we compare the cross-validated predictions with the |
| 91 | +# original data. This is appropriate as each prediction was generated in a |
| 92 | +# sample that did not include that observation. |
| 93 | + |
53 | 94 | test_y = np.tile(y, (fr_pred.shape[-1], 1)).T |
54 | 95 |
|
55 | 96 | rr_r2 = r2_score(test_y, rr_pred, multioutput="raw_values") |
|
0 commit comments