0% found this document useful (0 votes)
2 views1 page

4 LinearRegression

The document outlines a Python script that utilizes the Boston housing dataset to perform linear regression analysis. It includes data preprocessing steps, model training, and evaluation using mean squared error. The script demonstrates how to split the dataset into training and testing sets, fit the model, and check its accuracy.

Uploaded by

shashank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

4 LinearRegression

The document outlines a Python script that utilizes the Boston housing dataset to perform linear regression analysis. It includes data preprocessing steps, model training, and evaluation using mean squared error. The script demonstrates how to split the dataset into training and testing sets, fit the model, and check its accuracy.

Uploaded by

shashank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import pandas as pd

import numpy as np

from sklearn import linear_model


from sklearn.model_selection import train_test_split

from sklearn.datasets import fetch_openml


boston = fetch_openml(name='boston', version=1)
# still access the boston from skleanr

boston

df_x = pd.DataFrame(boston.data, columns = boston.feature_names)


df_x

df_x['CHAS']=df_x['CHAS'].astype('float64')
df_x['RAD']=df_x['RAD'].astype('float64')

df_y = pd.DataFrame(boston.target)
df_y

df_x.describe()

df_y.describe()

#init the linear regeression model


reg = linear_model.LinearRegression()

x_train, x_test, y_train, y_test = train_test_split(df_x, df_y, test_size=0.3,


random_state=42)

reg.fit(x_train,y_train)

print(reg.coef_)

y_pred = reg.predict(x_test)

y_pred

y_train

# Check Accuracy of model

print(np.mean(y_pred-y_test)**2)

from sklearn.metrics import mean_squared_error


print(mean_squared_error(y_test,y_pred))

# Both values should come same

You might also like