0% found this document useful (0 votes)
18 views2 pages

Multiple Linear Regression

Uploaded by

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

Multiple Linear Regression

Uploaded by

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

Linear Regression

In [1]: # import libraries


import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn import metrics

In [2]: # Read the dataset from csv file


df1 = pd.read_csv("C:/Users/DELL/Desktop/LR MLR Dataset.csv")
df1.head()

Out[2]: ID CRIM ZN INDUS CHAS NOX RM AGE DIS RAD TAX PTRATIO B LSTA

0 1 7.52601 0.0 18.10 0 0.713 6.417 98.3 2.1850 24 666 20.2 304.21 19.3

1 2 0.01360 75.0 4.00 0 0.410 5.888 47.6 7.3197 3 469 21.1 396.90 14.8

2 3 0.05789 12.5 6.07 0 0.409 5.878 21.4 6.4980 4 345 18.9 396.21 8.1

3 4 0.05646 0.0 12.83 0 0.437 6.232 53.7 5.0141 5 398 18.7 386.40 12.3

4 5 2.14918 0.0 19.58 0 0.871 5.709 98.5 1.6232 5 403 14.7 261.95 15.7

 

In [4]: # Features
X = df1.drop(['MEDV', 'ID'], axis = 1)
# Target
y = df1['MEDV']

# Create a model
lr = LinearRegression()
# Fit the model
lr.fit(X, y)
# make predictions
pred = lr.predict(X)

In [5]: print('Intercept:', lr.intercept_)

Intercept: 41.040928943858106

In [6]: coeff_df = pd.DataFrame(lr.coef_, X.columns, columns=['Coefficient'])


coeff_df
Out[6]: Coefficient

CRIM -0.106472

ZN 0.059236

INDUS 0.051623

CHAS 3.002702

NOX -18.613446

RM 2.993018

AGE 0.004273

DIS -1.432040

RAD 0.300060

TAX -0.013191

PTRATIO -0.916840

B 0.008668

LSTAT -0.529803

In [8]: print('Mean Absolute Error:', metrics.mean_absolute_error(y, pred))


print('Mean Squared Error:', metrics.mean_squared_error(y, pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y, pred)))
print('R2:', np.sqrt(metrics.r2_score(y, pred)))

Mean Absolute Error: 3.2249579483229565


Mean Squared Error: 21.53219881234212
Root Mean Squared Error: 4.640280035982971
R2: 0.8512690936204091

In [9]: features = [[0.06, 14, 6, 0, 0.5, 6, 22, 7, 4, 340, 19, 400, 8]]
print('The predicted price:', lr.predict(features)[0])

The predicted price: 19.41888951456918

In [ ]:

You might also like