<jupyter_start><jupyter_text>Linear Regression On Boston Data Set 1. House Prize Predication Data Set2. Collect The Data3. Prepare the Data4. Select the Data5. Descriptive Stast  Load The Packages<jupyter_code>import numpy as np
import pandas as pd 
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.datasets import load_boston<jupyter_output><empty_output><jupyter_text>Load The Data From Scikit-Learn<jupyter_code>Data = load_boston()
print(Data.DESCR)
df = pd.DataFrame(Data.data)
df
df.columns = Data.feature_names
df<jupyter_output><empty_output><jupyter_text>Exploratory Data Analysis<jupyter_code>df.head()
df.tail()
df.describe()
df.keys()
df.columns<jupyter_output><empty_output><jupyter_text>df.shape<jupyter_code>df['PRICE'] = Data.target
df.head()
df.shape
X = df.drop('PRICE',axis = 1)
X.shape
Y = df['PRICE']
Y.shape<jupyter_output><empty_output><jupyter_text>Train and Testing the Data<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.2)
x_train.shape
y_train.shape
x_test.shape
y_test.shape<jupyter_output><empty_output><jupyter_text>Linear Regression<jupyter_code>from sklearn.linear_model import LinearRegression
lm = LinearRegression()
lm.fit(x_train,y_train)
lm.coef_
lm.intercept_
y_pred = lm.predict(x_test)
y_pred.shape
y_pred
sns.set_style("whitegrid")
plt.scatter(y_test,y_pred)
plt.xlabel("Prices: $Y_i$")
plt.ylabel("Predicted Prices: $\hat{Y}_i$")<jupyter_output><empty_output><jupyter_text>Error Metrics and accuracy of model<jupyter_code>from sklearn.metrics import mean_squared_error,mean_absolute_error,r2_score
mean_absolute_error(y_test,y_pred)
mean_squared_error(y_test,y_pred)
r2_score(y_test,y_pred)
lm.score(x_train,y_train)
lm.score(x_test,y_test)
lm.score(x_test,y_pred)
lm_predication= lm.predict(x_test)
from sklearn.linear_model import Ridge,Lasso,ElasticNet,RidgeCV,LassoCV,ElasticNetCV<jupyter_output><empty_output><jupyter_text>Ridge and RidgeCv<jupyter_code>ridge = Ridge(alpha=0.1).fit(x_train,y_train)
ridge.fit(x_train,y_train)
ridge_predication = ridge.predict(x_test)
ridge_cv = RidgeCV(alphas=np.array([ 0.1, 0.001, 1. , 10. ])).fit(x_train,y_train)
ridge_cv_predication = ridge_cv.predict(x_test)<jupyter_output><empty_output><jupyter_text>Lasso and LassoCv<jupyter_code>lasso = Lasso(alpha=0.1).fit(x_train,y_train)
lasso_predication  = lasso.predict(x_test)
lasso_cv = LassoCV(alphas=np.array([0.1,0.01,0.001,1.0,10.])).fit(x_train,y_train)
lasso_cv_predication = lasso_cv.predict(x_test)<jupyter_output><empty_output><jupyter_text>ElasticNet,ElasticNetCV<jupyter_code>Enet = ElasticNet(alpha=0.1).fit(x_train,y_train)
Enet_Predication = Enet.predict(x_test)
Enet_cv = ElasticNetCV(alphas=[0.1,0.01,10.]).fit(x_train,y_train)
Enet_cv_predication = Enet_cv.predict(x_test)
All_Prediacation = pd.DataFrame({"actual_Data":y_test,
                                 'Linear':lm_predication,
                                  "ridge":ridge_predication,
                                  "ridge_cv":ridge_cv_predication,
                                 "Lasso":lasso_predication,
                                 "Lasso_cv":lasso_cv_predication,
                                 "ElasticNet":Enet_Predication,
                                 "ElasticNetCV":Enet_cv_predication
                                })
All_Prediacation
np.mean(All_Prediacation)
COEFF = pd.DataFrame({'Linear_Coeff':lm.coef_,
                      "ridge":ridge.coef_,
                      "ridge_cv":ridge_cv.coef_,
                      "lasso":lasso.coef_,
                      "Lasso_cv":lasso_cv.coef_,
                      "ElasticNet":Enet.coef_,
                      "ElasticNetCV":Enet_cv.coef_
                     })
from sklearn.model_selection import cross_val_score
models = [lm,ridge,ridge_cv,lasso,lasso_cv,Enet,Enet_cv]
for i in models:
    cv = cross_val_score(i, x_train,y_train,cv =20)
    print("Model name",i)
    print("Model Max",np.max(cv))
    print("Model Aaverage",np.mean(cv))
    print("Model Min",np.min(cv))
    print("*"*30)<jupyter_output>Model name LinearRegression()
Model Max 0.87482316712027
Model Aaverage 0.6949044624747763
Model Min 0.29056786333226214
******************************
Model name Ridge(alpha=0.1)
Model Max 0.8754485576530999
Model Aaverage 0.695102819047215
Model Min 0.28723172228797567
******************************
Model name RidgeCV(alphas=array([1.e-01, 1.e-03, 1.e+00, 1.e+01]))
Model Max 0.874830438747848
Model Aaverage 0.6941593919743136
Model Min 0.2872316892727027
******************************
Model name Lasso(alpha=0.1)
Model Max 0.8659995025367601
Model Aaverage 0.6834322846867031
Model Min 0.24260011954001803
******************************
Model name LassoCV(alphas=array([1.e-01, 1.e-02, 1.e-03, 1.e+00, 1.e+01]))
Model Max 0.8749098821827868
Model Aaverage 0.6949387284709536
Model Min 0.2896748427221342
******************************
Model name ElasticNet(alpha=0.1)
Model Max 0.8588118162693006
Model Aaverage 0.6834308511501934
Model Min 0.28950462936766463
******************************
M[...]
{'fraction_non_alphanumeric': 0.14392010965341687, 'fraction_numerical': 0.07597415312316429, 'mean_word_length': 4.752252252252252, 'pattern_counts': {'":': 13, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '137379', 'license': 'MIT', 'n_tokens_mistral': 2163, 'n_tokens_neox': 1909, 'n_words': 278, 'path': 'Regularization/Boston_DataSets(Linear,Lasso,Ridge,Elasticnet).ipynb', 'repo_name': 'vivekrathi14/Machine-Learning-with-Scikit-Learn-Python-3.x', 'size': '5107'}