Skip to content
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

[WIP] DOC - add example of scikit-learn GridSearchCV #52

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions examples/grid_search_cv.py
Original file line number Diff line number Diff line change
@@ -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_)