-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearRegression_Sklearn (1).py
128 lines (56 loc) · 1.35 KB
/
LinearRegression_Sklearn (1).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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
# coding: utf-8
# In[534]:
#Linear Reg. using sklearn
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# In[535]:
data=pd.read_csv(r'C:\Users\blankblack\Desktop\data_for_lr.csv')
# In[536]:
data.head(10)
# In[537]:
data.info()
# In[552]:
#handle null value
data=data.dropna()
# In[553]:
data.info()
# In[554]:
data.shape
# In[555]:
#splitting data
training_input=np.array(data.x[0:500]).reshape(500,1)
training_output=np.array(data.y[0:500]).reshape(500,1)
test_input=np.array(data.x[500:700]).reshape(199,1)
test_output=np.array(data.y[500:700]).reshape(199,1)
# In[558]:
#linear regression
#1.training model
from sklearn.linear_model import LinearRegression
linear_regressor=LinearRegression()
linear_regressor.fit(train_input,train_output)
# In[557]:
linear_regressor
# In[560]:
#predict test input
predicted_value=linear_regressor.predict(test_input.reshape(-1,1))
# In[561]:
predicted_value
# In[562]:
test_output
# In[564]:
from sklearn.metrics import mean_squared_error
error=mean_squared_error(test_output,predicted_value)
# In[565]:
error
# In[568]:
#visualizing error
#original hypo
plt.plot(test_input,test_output,'*',color='green')
#model hypo
plt.plot(test_input,predicted_value,'_',color='red')
plt.xlabel('input')
plt.ylabel('output')
plt.show()
# In[ ]: