Sklearn Ridge Regression | Ridge Regression Python

Ridge regression is similar to linear regression but it uses L2 regularization to reduce overfitting and increase model generalizability. We can quickly implement ridge regression in Python using the Sklearn library. In this tutorial, I will use the diabetes dataset for ridge regression because it is already available in Sklearn. I highly recommend this book for machine learning.

Step 1: Import the required libraries

from sklearn.datasets import load_diabetes
from sklearn.linear_model import Ridge
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error, mean_squared_error
import numpy as np

Step 2: Load the diabetes dataset

X, y = load_diabetes(return_X_y=True)

Step 3: Split the dataset into training and test set

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

Step 4: Fitting Ridge Regression Model on the training set

model = Ridge(alpha=0.5)
model.fit(X_train, y_train)

Step 5: Prediction on the test set

y_pred = model.predict(X_test)

Step 6: Model Evaluation using MAE, MSE, RMSE

print("MAE: ", mean_absolute_error(y_test, y_pred))
print("MSE: ", mean_squared_error(y_test, y_pred))
print("RMSE: ", np.sqrt(mean_squared_error(y_test, y_pred)))

Output:

MAE:  49.10068568923408
MSE:  3403.912386316557
RMSE:  58.34305773883091

Complete Code:

# Step 1: Import the required libraries
from sklearn.datasets import load_diabetes
from sklearn.linear_model import Ridge
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error, mean_squared_error
import numpy as np

# Step 2: Load the diabetes dataset
X, y = load_diabetes(return_X_y=True)

# Step 3: Split the dataset into the Training set and Test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Step 4: Fitting Ridge Regression Model to the Training set
model = Ridge(alpha=0.5)
model.fit(X_train, y_train)

# Step 5: Prediction on Test set
y_pred = model.predict(X_test)

# Step 6: Model Evaluation using MAE, MSE, RMSE
print("MAE: ", mean_absolute_error(y_test, y_pred))
print("MSE: ", mean_squared_error(y_test, y_pred))
print("RMSE: ", np.sqrt(mean_squared_error(y_test, y_pred)))

Related Articles:

Machine Learning – AiHints

Leave a Comment

Your email address will not be published.