Ridge and Lasso Regression Practical Implementation in Python

Alok Choudhary
2 min readApr 25, 2024

--

Linear Regression

Understanding Linear Regression:

Before diving into Ridge and Lasso regression, recap linear regression. click here for understand Linear Regression.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_openml
df = fetch_openml(name='boston')
dataset=pd.DataFrame(df.data)
dataset
# Independent features and dependent features
X=dataset
y=df.target
# train test split
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.30, random_state=42)
# standardizing the dataset
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train=scaler.fit_transform(X_train)
X_test=scaler.transform(X_test)
from sklearn.linear_model import LinearRegression
#cross validation
from sklearn.model_selection import cross_val_score
regression=LinearRegression()
regression.fit(X_train,y_train)
mse=cross_val_score(regression,X_train,y_train,scoring='neg_mean_squared_error',cv=10)
np.mean(mse)
##prediction
reg_pred=regression.predict(X_test)
import seaborn as sns
sns.displot(reg_pred-y_test,kind='kde')
from sklearn.metrics import r2_score
score=r2_score(reg_pred,y_test)
score

Ridge Regression Algorithm

Recap Ridge Regression :- Click here to get complete Mathematical Intuition behind ridge and lasso regression.

from sklearn.linear_model import Ridge
from sklearn.model_selection import GridSearchCV
ridge_regressor=Ridge()
ridge_regressor
parameters={'alpha':[1,2,5,10,20,30,40,50,60,70,80,90]}
ridgecv=GridSearchCV(ridge_regressor,parameters,scoring='neg_mean_squared_error',cv=5)
ridgecv.fit(X_train,y_train)
print(ridgecv.best_params_)
print(ridgecv.best_score_)
ridge_pred=ridgecv.predict(X_test)
import seaborn as sns
sns.displot(ridge_pred-y_test,kind='kde')
from sklearn.metrics import r2_score
score=r2_score(ridge_pred,y_test)
score

Lasso Regression Algorithm

Recap Lasso Regression :- Click here to get complete Mathematical Intuition behind ridge and lasso regression.

# Lasso Regression
from sklearn.linear_model import Lasso
lasso=Lasso()
parameters={'alpha':[1,2,5,10,20,30,40,50,60,70,80,90]}
lassocv=GridSearchCV(lasso,parameters,scoring='neg_mean_squared_error',cv=5)
lassocv.fit(X_train,y_train)
print(lassocv.best_params_)
print(lassocv.best_score_)
lasso_pred=lassocv.predict(X_test)
import seaborn as sns
sns.displot(lasso_pred-y_test,kind='kde')
print("Thank-You!Happy Learning!")

Join me in exploring these pillars of technological evolution. Let’s unravel the mysteries, debunk the myths, and harness the power of data to shape our future. Follow my journey, engage with the visuals, and let’s decode the future, one pixel at a time.

--

--

Alok Choudhary
Alok Choudhary

Written by Alok Choudhary

My Self Alok Choudhary, a Data Science scholar at IIT Patna, is pioneering in AI, ML, DS, and DL, crafting algorithms that redefine the tech landscape.

No responses yet