-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearRegression.py
More file actions
38 lines (30 loc) · 997 Bytes
/
LinearRegression.py
File metadata and controls
38 lines (30 loc) · 997 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
import math
#read csv
dataset = pd.read_csv("datasets/house_prices.csv")
size=dataset['sqft_living']
price=dataset['price']
print(price)
#machine learing handle arrays not dataframes
# we need array with single item
x = np.array(size).reshape(-1,1)
y = np.array(price).reshape(-1,1)
model = LinearRegression()
model.fit(x, y) # sklearn is going to train the model with help of fradient descent
#this is the b1
print(model.coef_[0])
#this is b0 in our model
print(model.intercept_[0])
#visualize the dataset with the fitted model
plt.scatter(x, y, color= 'green')
plt.plot(x, model.predict(x), color = 'black')
plt.title ("Linear Regression")
plt.xlabel("Size")
plt.ylabel("Price")
plt.show()
#Predicting the prices
print("Prediction by the model:" , model.predict([[2000]])) #Prediction by the model: [[517666.39270042]]