image_output_1.png
from sklearn import metrics

from sklearn.linear_model import Lasso

from sklearn.linear_model import LinearRegression

from sklearn.linear_model import Ridge

from sklearn.linear_model import Ridge

from sklearn.model_selection import GridSearchCV

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

names = ['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX', 'PTRATIO', 'B', 'LSTAT', 'MEDV']
data_df = pd.read_csv('/kaggle/input/boston-house-prices/housing.csv', header=None, delim_whitespace=True, names=names)

from sklearn.linear_model import LinearRegression
lr_all = LinearRegression()
lr_all.fit(X_train, y_train)
y_pred1 = lr_all.predict(X_test)

lr_all.intercept_

lr_all_coeffcients = pd.DataFrame([X_train.columns, lr_all.coef_]).T
lr_all_coeffcients = lr_all_coeffcients.rename(columns={0: 'Attribute', 1: 'Coefficients'})
lr_all_coeffcients

from sklearn import metrics

from sklearn.linear_model import Ridge
ridge = Ridge(alpha=100)
ridge.fit(X_train, y_train)
y_pred2 = ridge.predict(X_test)

ridge.score(X_test, y_test)

from sklearn.linear_model import Lasso
lasso = Lasso(alpha=0.8)
lasso.fit(X_train, y_train)
y_pred3 = lasso.predict(X_test)
lasso.score(X_test, y_test)

lasso_coeffcients = pd.DataFrame([X_train.columns, lasso.coef_]).T
lasso_coeffcients = lasso_coeffcients.rename(columns={0: 'Attribute', 1: 'Coefficients'})
lasso_coeffcients

from sklearn.model_selection import GridSearchCV
param_grid = {'alpha': np.arange(1, 10, 500)}
ridge = Ridge()
ridge_best_alpha = GridSearchCV(ridge, param_grid)
ridge_best_alpha.fit(X_train, y_train)

print('Best alpha for Ridge Regression:', ridge_best_alpha.best_params_)
print('Best score for Ridge Regression with best alpha:', ridge_best_alpha.best_score_)