<jupyter_start><jupyter_text>Import the Required Libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns<jupyter_output><empty_output><jupyter_text>Load the Dataset<jupyter_code>from sklearn.datasets import load_boston
df = load_boston()
df
dataset = pd.DataFrame(df.data)
print(dataset.head())
dataset.columns = df.feature_names
dataset.head()
df.target.shape
dataset['Price'] = df.target
dataset.head()<jupyter_output><empty_output><jupyter_text>Split the Dataset<jupyter_code>X = dataset.iloc[:, :-1]
y = dataset.iloc[:, -1]<jupyter_output><empty_output><jupyter_text>Linear Regression<jupyter_code>from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LinearRegression
lin_regressor = LinearRegression()
mse = cross_val_score(lin_regressor, X, y, scoring='neg_mean_squared_error', cv=5)
mean_mse = np.mean(mse)
print(mse)
print(mean_mse)<jupyter_output>[-12.46030057 -26.04862111 -33.07413798 -80.76237112 -33.31360656]
-37.13180746769906<jupyter_text>Ridge Regression<jupyter_code>from sklearn.linear_model import Ridge
from sklearn.model_selection import GridSearchCV
ridge = Ridge()
parameters = {'alpha':[1e-15,1e-10,1e-8,1e-3,1e-2,1,5,10,20,30,35,40,45,50,55,100]}
ridge_regressor = GridSearchCV(ridge, parameters, scoring = 'neg_mean_squared_error', cv=5)
ridge_regressor.fit(X, y)
print(ridge_regressor.best_params_)
print(ridge_regressor.best_score_)<jupyter_output>{'alpha': 100}
-29.905701947540212<jupyter_text>Lasso Regression<jupyter_code>from sklearn.linear_model import Lasso
from sklearn.model_selection import GridSearchCV
lasso = Lasso()
parameters = {'alpha':[1e-15,1e-10,1e-8,1e-3,1e-2,1,5,10,20,30,35,40,45,50,55,100]}
lasso_regressor = GridSearchCV(lasso, parameters, scoring = 'neg_mean_squared_error', cv=5)
lasso_regressor.fit(X, y)
print(lasso_regressor.best_params_)
print(lasso_regressor.best_score_)<jupyter_output>{'alpha': 1}
-35.53158022069486<jupyter_text>Split the Dataset into train test split<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 0)<jupyter_output><empty_output><jupyter_text>Predict the data in both the Regressor<jupyter_code>prediction_lasso = lasso_regressor.predict(X_test)
print(prediction_lasso)
prediction_ridge = ridge_regressor.predict(X_test)
print(prediction_ridge)<jupyter_output>[23.96675946 24.98996014 27.5915891  14.26059397 19.63842944 21.33417836
 21.32039718 23.27239931 20.30837677 19.48226179  6.4578138  14.28838187
 18.59820449  7.32675586 36.99591747 31.36360797 22.27052891 35.16096521
 30.99397818 24.25137539 25.40646048 24.34913685 20.21632652 30.0992825
 22.68378433 13.94885033 18.64684143 21.48729544 33.26170271 19.79217513
 18.17155893 19.9695722  20.42970393 23.50528313 28.90147149 20.77821424
 11.23603614 25.21197341 16.33605596 13.74459083 24.96162953 20.32082943
 21.56714797 16.00431973 22.631114   24.95003631 18.4770964  24.0302334
 12.20742324 24.00507962 21.73290773 17.56687132 24.74921266 28.65328508
 13.58721717 21.3260628  20.50282091 15.43442841 15.56550274 19.58519135
 18.94927305 21.87657392 31.86147714 31.60936099 18.30851646 31.83190056
 19.49024654 18.37266891 17.08246064 22.88948035 21.00301604 22.55678293
 30.73421263 29.37650392 25.53367669  5.7995898  35.17839807 23.37460756
 27.00341299 20.13591331 28.06232493 18.88117326 21.1[...]<jupyter_text>Visualize the prediction<jupyter_code>sns.distplot(y_test-prediction_lasso)
sns.distplot(y_test-prediction_ridge)<jupyter_output>C:\Users\<NAME>\anaconda3\lib\site-packages\seaborn\distributions.py:2551: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)
{'fraction_non_alphanumeric': 0.10745058286872783, 'fraction_numerical': 0.2513938165230613, 'mean_word_length': 7.222916666666666, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '172876', 'license': 'MIT', 'n_tokens_mistral': 2210, 'n_tokens_neox': 1568, 'n_words': 269, 'path': 'Ridge & Lasso Regression Basics.ipynb', 'repo_name': 'DevmallyaK/Advanced-Regression-Basic', 'size': '3955'}