SKLearner Home | About | Contact | Examples

Scikit-Learn RidgeCV Regression Model

RidgeCV is a regression algorithm that incorporates built-in cross-validation to automatically select the best regularization strength. This is useful for regression tasks where tuning the alpha parameter can significantly impact model performance.

The key hyperparameters of RidgeCV include alphas (a list of alpha values for cross-validation) and cv (the cross-validation splitting strategy).

The algorithm is appropriate for regression problems.

from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.linear_model import RidgeCV
from sklearn.metrics import mean_squared_error

# generate regression dataset
X, y = make_regression(n_samples=100, n_features=5, noise=0.1, random_state=1)

# split into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)

# define model with cross-validation
model = RidgeCV(alphas=[0.1, 1.0, 10.0], cv=5)

# fit model
model.fit(X_train, y_train)

# evaluate model
yhat = model.predict(X_test)
mse = mean_squared_error(y_test, yhat)
print('MSE: %.3f' % mse)

# make a prediction
row = [[-0.00666354, 1.89679179, -0.74415919, -0.55715973, 1.69504583]]
yhat = model.predict(row)
print('Predicted: %.3f' % yhat[0])

Running the example gives an output like:

MSE: 0.021
Predicted: 90.106

The steps are as follows:

  1. Generate a synthetic regression dataset using the make_regression() function. This creates a dataset with specified parameters such as the number of samples (n_samples), noise level (noise), and a fixed random seed (random_state) for reproducibility. The dataset is split into training and test sets using train_test_split().

  2. Define the RidgeCV model with a range of alpha values for cross-validation (alphas) and specify the cross-validation splitting strategy (cv).

  3. Fit the model on the training data using the fit() method.

  4. Evaluate the model’s performance by predicting on the test set and calculating the mean squared error using the mean_squared_error() function.

  5. Make a single prediction by passing a new data sample to the predict() method.

This example demonstrates how to set up and use a RidgeCV model for regression tasks, showcasing its ability to automatically tune hyperparameters through cross-validation in scikit-learn.



See Also