Linear Regression
Understanding Linear Regression:
Sooner 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(establish='boston')
dataset=pd.DataFrame(df.info)
dataset
# Neutral choices and dependent choices
X=dataset
y=df.objective
# observe examine lower 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.rework(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.indicate(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
ranking=r2_score(reg_pred,y_test)
ranking
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,kind='kde')
from sklearn.metrics import r2_score
ranking=r2_score(ridge_pred,y_test)
ranking
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,kind='kde')
print("Thank-You!Comfy Learning!")
Be a part of me in exploring these pillars of technological evolution. Let’s unravel the mysteries, debunk the myths, and harness the flexibility of data to type our future. Follow my journey, engage with the visuals, and let’s decode the long term, one pixel at a time.