Linear Regression
Understanding Linear Regression:
Earlier than 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(identify='boston')
dataset=pd.DataFrame(df.information)
dataset
# Impartial options and dependent options
X=dataset
y=df.goal
# practice check cut up
from sklearn.model_selection import train_test_splitX_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.remodel(X_test)
from sklearn.linear_model import LinearRegression
#cross validation
from sklearn.model_selection import cross_val_score
regression=LinearRegression()
regression.match(X_train,y_train)
mse=cross_val_score(regression,X_train,y_train,scoring='neg_mean_squared_error',cv=10)
np.imply(mse)
##prediction
reg_pred=regression.predict(X_test)
import seaborn as sns
sns.displot(reg_pred-y_test,type='kde')
from sklearn.metrics import r2_score
rating=r2_score(reg_pred,y_test)
rating
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.match(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,type='kde')
from sklearn.metrics import r2_score
rating=r2_score(ridge_pred,y_test)
rating
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.match(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,type='kde')
print("Thank-You!Comfortable Studying!")
Be part of me in exploring these pillars of technological evolution. Let’s unravel the mysteries, debunk the myths, and harness the ability of knowledge to form our future. Follow my journey, have interaction with the visuals, and let’s decode the longer term, one pixel at a time.