-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simple_Linear_Regression.py
48 lines (36 loc) · 1.01 KB
/
Simple_Linear_Regression.py
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
39
40
41
42
43
44
45
46
47
48
import numpy as np
from sklearn.linear_model import LinearRegression
x = np.array([5,15,25,35,35,55]).reshape((-1,1))
y = np.array([5,20,14,32,22,38])
#print(x,'\n',y)
# Model Selection
model = LinearRegression()
model.fit(x,y)
model=LinearRegression().fit(x, y)
print(model)
# find the Coefficents
r_sq = model.score(x,y)
print("Coefficents of X,Y",r_sq)
# find the interception
print("Intercept:",model.intercept_)
print("Slope:",model.coef_)
# Passing the Data to model
new_model = LinearRegression().fit(x,y.reshape(-1,1))
print("Intercepts:",new_model.intercept_)
print('slope',new_model.coef_)
# Find the Y values
y_pred=model.predict(x)
print('Predicted Response',y_pred)
# OR
y_pred=model.intercept_+model.coef_*x
print("Predicted Response",y_pred,sep='\n')
# New Input to Test
x_new=np.arange(6).reshape((-1,1))
#print(x_new)
y_new=model.predict(x_new)
print(y_new)
# Data Visulization
import matplotlib.pyplot as plt
plt.scatter(x,y)
plt.plot(x_new,y_new)
plt.show()