-
Notifications
You must be signed in to change notification settings - Fork 0
/
Regression.py
155 lines (80 loc) · 2.05 KB
/
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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
import pandas as pd
from matplotlib import pyplot as ply
from sklearn.datasets import load_boston
# In[54]:
#understanding dataset
boston=load_boston()
print(boston.DESCR)
# In[10]:
#access data attributes
dataset = boston.data
for name,index in enumerate(boston.feature_names):
print(index,name)
# In[18]:
#reshaping data
data =dataset[:,12].reshape(-1,1)
# In[19]:
#shape of data
np.shape(data)
# In[16]:
#target values
target=boston.target.reshape(-1,1)
# In[17]:
#shape of target
np.shape(target)
# In[26]:
#ensuring that matplotlib is working inside the notebook
get_ipython().run_line_magic('matplotlib', 'inline')
ply.scatter(data,target,color='green')
ply.xlabel('Lower income population')
ply.ylabel('Cost of House')
ply.show()
# In[44]:
#regression
#from sklearn.linear_model import LinearRegression
#from sklearn.linear_model import Lasso
from sklearn.linear_model import Ridge
#creating a regression model
reg=Ridge()
#fit model
reg.fit(data,target)
# In[45]:
#prediction
pred=reg.predict(data)
# In[46]:
#ensuring that matplotlib is working inside the notebook
get_ipython().run_line_magic('matplotlib', 'inline')
ply.scatter(data,target,color='red')
ply.plot(data,pred,color='green')
ply.xlabel('Lower income population')
ply.ylabel('Cost of House')
ply.show()
# In[47]:
#circumventing curve using polynomial model
from sklearn.preprocessing import PolynomialFeatures
#to allow merging of models
from sklearn.pipeline import make_pipeline
# In[48]:
model=make_pipeline(PolynomialFeatures(7),reg)
# In[49]:
model.fit(data,target)
# In[50]:
pred=model.predict(data)
# In[51]:
#ensuring that matplotlib is working inside the notebook
get_ipython().run_line_magic('matplotlib', 'inline')
ply.scatter(data,target,color='red')
ply.plot(data,pred,color='green')
ply.xlabel('Lower income population')
ply.ylabel('Cost of House')
ply.show()
# In[52]:
# r_2 metric
from sklearn.metrics import r2_score
# In[53]:
#prediction
r2_score(pred,target)