MNT Speedup example plot_select_from_model_diabetes.py
@@ -36,7 +36,7 @@ print(diabetes.DESCR)
# ------------------------------------
#
# To get an idea of the importance of the features, we are going to use the
-# :class:`~sklearn.linear_model.LassoCV` estimator. The features with the
+# :class:`~sklearn.linear_model.RidgeCV` estimator. The features with the
# highest absolute `coef_` value are considered the most important.
# We can observe the coefficients directly without needing to scale them (or
# scale the data) because from the description above, we know that the features
@@ -46,10 +46,10 @@ print(diabetes.DESCR)
# :ref:`sphx_glr_auto_examples_inspection_plot_linear_model_coefficient_interpretation.py`.
import matplotlib.pyplot as plt
import numpy as np
-from sklearn.linear_model import LassoCV
+from sklearn.linear_model import RidgeCV
-lasso = LassoCV().fit(X, y)
-importance = np.abs(lasso.coef_)
+ridge = RidgeCV(alphas=np.logspace(-6, 6, num=5)).fit(X, y)
+importance = np.abs(ridge.coef_)
feature_names = np.array(diabetes.feature_names)
plt.bar(height=importance, x=feature_names)
plt.title("Feature importances via coefficients")
@@ -73,7 +73,7 @@ from time import time
threshold = np.sort(importance)[-3] + 0.01
tic = time()
-sfm = SelectFromModel(lasso, threshold=threshold).fit(X, y)
+sfm = SelectFromModel(ridge, threshold=threshold).fit(X, y)
toc = time()
print(f"Features selected by SelectFromModel: {feature_names[sfm.get_support()]}")
print(f"Done in {toc - tic:.3f}s")
@@ -98,13 +98,13 @@ from sklearn.feature_selection import SequentialFeatureSelector
tic_fwd = time()
sfs_forward = SequentialFeatureSelector(
- lasso, n_features_to_select=2, direction="forward"
+ ridge, n_features_to_select=2, direction="forward"
).fit(X, y)
toc_fwd = time()
tic_bwd = time()
sfs_backward = SequentialFeatureSelector(
- lasso, n_features_to_select=2, direction="backward"
+ ridge, n_features_to_select=2, direction="backward"
).fit(X, y)
toc_bwd = time()
