-
-
Notifications
You must be signed in to change notification settings - Fork 26.3k
FIX/API Fix AIC/BIC in LassoLarsIC and introduce noise_variance #21481
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
Merged
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
c3839d9
FIX compute the AIC and BIC using MSE
glemaitre b46e6ee
iter
glemaitre 053db7c
Merge remote-tracking branch 'origin/main' into is/14566
glemaitre e864611
add changelog
glemaitre 0b61363
acknowledge
glemaitre 776490e
iter
glemaitre caea26c
iter
glemaitre 979f629
iter
glemaitre 61a6b6c
DOC add a new example
glemaitre 668461a
iter
glemaitre 880edbf
iter
glemaitre e3ef92b
iter
glemaitre d81bca9
iter
glemaitre ecb1401
Merge branch 'main' into is/14566
glemaitre 97c3259
Apply suggestions from code review
glemaitre ee7b6a4
iter
glemaitre fada91c
Apply suggestions from code review
glemaitre 25e3e6e
Update sklearn/linear_model/_least_angle.py
glemaitre 794418a
iter
glemaitre 30386c4
iter
glemaitre 228ca79
Apply suggestions from code review
glemaitre 10ececf
iter
glemaitre e353b6a
Apply suggestions from code review
glemaitre 1c07447
Apply suggestions from code review
glemaitre 1eaee6f
Merge remote-tracking branch 'origin/main' into is/14566
glemaitre 47be241
iter
glemaitre 121b48f
iter
glemaitre 50c862d
iter
glemaitre 37c2c93
iter
glemaitre 73a9f5e
christian improvements
glemaitre e3dc244
last review
glemaitre 227f2a1
last review
glemaitre 19f9032
black
glemaitre e3a4daf
Merge remote-tracking branch 'origin/main' into is/14566
glemaitre d282d94
Apply suggestions from code review
glemaitre 369f6a7
iter
glemaitre e426eb4
Apply suggestions from code review
glemaitre 6f08ddf
reviews
glemaitre e347478
Update examples/linear_model/plot_lasso_model_selection.py
glemaitre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
""" | ||
============================================== | ||
Lasso model selection via information criteria | ||
============================================== | ||
glemaitre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
This example reproduces the example of Fig. 2 of [ZHT2007]_. A | ||
:class:`~sklearn.linear_model.LassoLarsIC` estimator is fit on a | ||
diabetes dataset and the AIC and the BIC criteria are used to select | ||
the best model. | ||
|
||
.. note:: | ||
It is important to note that the optimization to find `alpha` with | ||
:class:`~sklearn.linear_model.LassoLarsIC` relies on the AIC or BIC | ||
criteria that are computed in-sample, thus on the training set directly. | ||
This approach differs from the cross-validation procedure. For a comparison | ||
of the two approaches, you can refer to the following example: | ||
:ref:`sphx_glr_auto_examples_linear_model_plot_lasso_model_selection.py`. | ||
|
||
.. topic:: References | ||
|
||
.. [ZHT2007] :arxiv:`Zou, Hui, Trevor Hastie, and Robert Tibshirani. | ||
"On the degrees of freedom of the lasso." | ||
The Annals of Statistics 35.5 (2007): 2173-2192. | ||
<0712.0881>` | ||
""" | ||
|
||
# Author: Alexandre Gramfort | ||
# Guillaume Lemaitre | ||
# License: BSD 3 clause | ||
|
||
# %% | ||
import sklearn | ||
|
||
sklearn.set_config(display="diagram") | ||
|
||
# %% | ||
# We will use the diabetes dataset. | ||
from sklearn.datasets import load_diabetes | ||
|
||
X, y = load_diabetes(return_X_y=True, as_frame=True) | ||
n_samples = X.shape[0] | ||
X.head() | ||
|
||
# %% | ||
# Scikit-learn provides an estimator called | ||
# :class:`~sklearn.linear_model.LinearLarsIC` that uses either Akaike's | ||
# information criterion (AIC) or the Bayesian information criterion (BIC) to | ||
# select the best model. Before fitting | ||
# this model, we will scale the dataset. | ||
# | ||
# In the following, we are going to fit two models to compare the values | ||
# reported by AIC and BIC. | ||
from sklearn.preprocessing import StandardScaler | ||
from sklearn.linear_model import LassoLarsIC | ||
from sklearn.pipeline import make_pipeline | ||
|
||
lasso_lars_ic = make_pipeline( | ||
StandardScaler(), LassoLarsIC(criterion="aic", normalize=False) | ||
).fit(X, y) | ||
|
||
|
||
# %% | ||
# To be in line with the defintion in [ZHT2007]_, we need to rescale the | ||
# AIC and the BIC. Indeed, Zou et al. are ignoring some constant terms | ||
# compared to the original definition of AIC derived from the maximum | ||
# log-likelihood of a linear model. You can refer to | ||
# :ref:`mathematical detail section for the User Guide <lasso_lars_ic>`. | ||
def zou_et_al_criterion_rescaling(criterion, n_samples, noise_variance): | ||
"""Rescale the information criterion to follow the definition of Zou et al.""" | ||
return criterion - n_samples * np.log(2 * np.pi * noise_variance) - n_samples | ||
|
||
|
||
# %% | ||
import numpy as np | ||
|
||
aic_criterion = zou_et_al_criterion_rescaling( | ||
lasso_lars_ic[-1].criterion_, | ||
n_samples, | ||
lasso_lars_ic[-1].noise_variance_, | ||
) | ||
|
||
index_alpha_path_aic = np.flatnonzero( | ||
lasso_lars_ic[-1].alphas_ == lasso_lars_ic[-1].alpha_ | ||
)[0] | ||
|
||
# %% | ||
lasso_lars_ic.set_params(lassolarsic__criterion="bic").fit(X, y) | ||
|
||
bic_criterion = zou_et_al_criterion_rescaling( | ||
lasso_lars_ic[-1].criterion_, | ||
n_samples, | ||
lasso_lars_ic[-1].noise_variance_, | ||
) | ||
|
||
index_alpha_path_bic = np.flatnonzero( | ||
lasso_lars_ic[-1].alphas_ == lasso_lars_ic[-1].alpha_ | ||
)[0] | ||
|
||
# %% | ||
# Now that we collected the AIC and BIC, we can as well check that the minima | ||
# of both criteria happen at the same alpha. Then, we can simplify the | ||
# following plot. | ||
index_alpha_path_aic == index_alpha_path_bic | ||
|
||
# %% | ||
# Finally, we can plot the AIC and BIC criterion and the subsequent selected | ||
# regularization parameter. | ||
import matplotlib.pyplot as plt | ||
|
||
plt.plot(aic_criterion, color="tab:blue", marker="o", label="AIC criterion") | ||
plt.plot(bic_criterion, color="tab:orange", marker="o", label="BIC criterion") | ||
plt.vlines( | ||
index_alpha_path_bic, | ||
aic_criterion.min(), | ||
aic_criterion.max(), | ||
color="black", | ||
linestyle="--", | ||
label="Selected alpha", | ||
) | ||
plt.legend() | ||
plt.ylabel("Information criterion") | ||
plt.xlabel("Lasso model sequence") | ||
_ = plt.title("Lasso model selection via AIC and BIC") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.