You are on page 1of 3

1/3/23, 2:10 PM p1

Linear Regression implementation model for


House-Price Prediction System
In [1]: import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model

In [12]: df=pd.read_csv("homeprices.csv")
#df.head(2)
#df.tail()
#df.head()
#df.tail(2)
#df.describe()
#df.info()
#df.shape
df

Out[12]: area price

0 2600 550000

1 3000 565000

2 3200 610000

3 3600 680000

4 4000 725000

In [15]: %matplotlib inline


plt.xlabel("Area(sq.ft)")
plt.ylabel('Price($)')
plt.scatter(df['area'],df.price,color='red',marker='*')
df['area']

0 2600
Out[15]:
1 3000
2 3200
3 3600
4 4000
Name: area, dtype: int64

file:///home/invincible/Desktop/p1.html 1/3
1/3/23, 2:10 PM p1

In [16]: reg=linear_model.LinearRegression()
reg.fit(df[['area']],df.price)

LinearRegression()
Out[16]:

In [17]: new_data=np.array(3300).reshape(-1,1)
new_data
y_pred=reg.predict(new_data)
y_pred
new_data1=np.array(3700).reshape(-1,1)
new_data1
y_pred1=reg.predict(new_data1)
y_pred1

/home/invincible/anaconda3/lib/python3.9/site-packages/sklearn/base.py:450:
UserWarning: X does not have valid feature names, but LinearRegression was
fitted with feature names
warnings.warn(
/home/invincible/anaconda3/lib/python3.9/site-packages/sklearn/base.py:450:
UserWarning: X does not have valid feature names, but LinearRegression was
fitted with feature names
warnings.warn(
array([683030.82191781])
Out[17]:

In [18]: print("Cofficient{}:",reg.coef_)

Cofficient{}: [135.78767123]

In [19]: print("Intercept{}:",reg.intercept_)

Intercept{}: 180616.43835616432

In [22]: %matplotlib inline


plt.xlabel("Area(sq.ft)")
plt.ylabel('Price($)')

file:///home/invincible/Desktop/p1.html 2/3
1/3/23, 2:10 PM p1
plt.scatter(df.area,df.price,color='red',marker='*')
plt.plot(df.area,reg.predict(df[['area']]),color='blue')

[<matplotlib.lines.Line2D at 0x7f5f1eb03910>]
Out[22]:

In [ ]:

file:///home/invincible/Desktop/p1.html 3/3

You might also like