From 32b3c496664b94572654fc113519be891559b0b2 Mon Sep 17 00:00:00 2001 From: Badr MOUFAD Date: Fri, 19 Aug 2022 17:35:35 +0200 Subject: [PATCH] add example ``gridsearchcv`` --- examples/grid_search_cv.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 examples/grid_search_cv.py diff --git a/examples/grid_search_cv.py b/examples/grid_search_cv.py new file mode 100644 index 000000000..81bae1ac0 --- /dev/null +++ b/examples/grid_search_cv.py @@ -0,0 +1,35 @@ +""" +========================================== +Skglm support of scikit-learn GridSearchCV +========================================== +An example that uses scikit-learn``GridSearchCV`` to select +the best ``alpha`` and ``l1_ratio`` of ElasticNet model. +""" + +import numpy as np +from skglm import ElasticNet +from skglm.utils import make_correlated_data +from sklearn.model_selection import GridSearchCV + + +# Simulate dataset +n_samples, n_features = 10, 100 +X, y, _ = make_correlated_data(n_samples, n_features, random_state=0) + +# grid of parameter +alpha_max = np.linalg.norm(X.T @ y, ord=np.inf) / n_samples +parameters = { + 'alpha': alpha_max * np.geomspace(1, 1e-3, 100), + 'l1_ratio': [1., 0.9, 0.8, 0.7] +} + +# init and fit GridSearchCV +reg = GridSearchCV( + ElasticNet(), + param_grid=parameters, + cv=5, n_jobs=-1 +) +reg.fit(X, y) + +# print the best parameters +print(reg.best_estimator_)