-
Notifications
You must be signed in to change notification settings - Fork 0
/
hp_support_vector_regression.py
313 lines (160 loc) · 5.16 KB
/
hp_support_vector_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env python
# coding: utf-8
# # Support Vector Regression (SVR)
# ## Importing the libraries
# In[1]:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import qgrid
# Make sure all print() lines are printed, not just the last one
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
# Make sure matplotlib charts and graphs are displayed in the cell outputs
get_ipython().run_line_magic('matplotlib', 'inline')
# ## Importing the dataset
# >
# > This notebook expects cleaned data
# > See "EDA.ipynb" - used to clean data
# In[2]:
dataset = pd.read_csv("cleaned_data.csv")
# dataset.info()
# In[3]:
# dataset.iloc[0:10, 0]
# In[4]:
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values
# In[5]:
y = y.reshape(len(y), 1)
# In[6]:
np.shape(X)
# In[7]:
np.shape(y)
# ## Splitting the dataset into the Training set and Test set
# In[8]:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# In[9]:
# np.shape(X_train)
# # np.shape(X_test)
# # np.shape(y_train)
# # np.shape(y_test)
# ## Feature Scaling
# In[10]:
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
sc_y = StandardScaler()
X_train = sc_X.fit_transform(X_train)
y_train = sc_y.fit_transform(y_train)
# ## Training the SVR model on the Training set
# In[11]:
type(y_train)
# In[12]:
y_train = y_train.ravel() # Note the .ravel() to convert into one dimension
# In[13]:
# np.shape(y_train)
# In[15]:
from sklearn.svm import SVR
regressor = SVR(kernel="rbf")
regressor.fit(X_train, y_train)
type(y_train)
# ## Predicting the Test set results
# In[16]:
y_pred = sc_y.inverse_transform(regressor.predict(sc_X.transform(X_test)))
np.set_printoptions(precision=2)
# print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))
# ## Evaluating the Model Performance
# >######################################################################################################
# >######################################################################################################
# In[18]:
# %%script
# from sklearn.metrics import r2_score
# r2_score(y_test, y_pred)
# In[19]:
get_ipython().run_line_magic('lsmagic', '')
# >######################################################################################################
# >######################################################################################################
# # Make competition submission
# In[20]:
# Import data
data = pd.read_csv("hp_test.csv")
# data.info()
# In[21]:
# Keep only the features as decided through the EDA
keep_numerical = [
"GrLivArea",
"GarageArea",
"TotalBsmtSF",
"1stFlrSF",
"TotRmsAbvGrd",
"YearBuilt",
"YearRemodAdd",
]
keep_categorical = [
"OverallQual",
"Neighborhood",
"GarageCars",
"ExterQual",
"BsmtQual",
"KitchenQual",
"FullBath",
"GarageFinish",
"FireplaceQu",
"Foundation",
"GarageType",
]
data_subset = data[keep_categorical + keep_numerical]
# data_subset.info()
# In[22]:
# qgrid_widget = qgrid.show_grid(pd.DataFrame(data_subset),
# show_toolbar=True,
# grid_options={'forceFitColumns': False} # Many columns in dataframe become unusable for filter without this
# )
# qgrid_widget
# In[23]:
# Deal with missing data
data_subset.drop(columns="FireplaceQu", inplace=True)
# In[24]:
# Encode categorical features
data_subset_enc = pd.get_dummies(data_subset)
# In[25]:
# qgrid_widget = qgrid.show_grid(pd.DataFrame(data_subset_enc),
# show_toolbar=True,
# grid_options={'forceFitColumns': False} # Many columns in dataframe become unusable for filter without this
# )
# qgrid_widget
# In[26]:
# data_subset_enc.info()
# In[27]:
# Now prepare for competition submission
##### Confirm why first column is dropped ##########
X_competition = data_subset_enc.iloc[:, :].values
# In[28]:
np.shape(X_competition)
# In[29]:
# Now only have numeric data, so impute missing values
from sklearn.impute import SimpleImputer
imputer = SimpleImputer(missing_values=np.nan, strategy="mean")
imputer.fit(X_competition)
X_competition = imputer.transform(X_competition)
# np.shape(X_competition)
# In[30]:
y_competition = sc_y.inverse_transform(regressor.predict(sc_X.transform(X_competition)))
# np.shape(y_competition)
# In[31]:
# dataset.iloc[0:10, 0]
# In[32]:
# Concatenate prediction and passenger ID columns
# submission = pd.DataFrame(pd.concat([dataset.iloc[:, 0], y_competition.iloc[:, :]], axis=1))
submission = pd.DataFrame(y_competition)
# In[33]:
submission.columns = ['SalePrice']
IdColumn = pd.read_csv("hp_test.csv")
# submission = pd.concat(IdColumn["Id"], submission["SalePrice"])
# submission
submission = pd.concat([IdColumn['Id'], submission['SalePrice']], axis=1)
#print(submission)
# In[34]:
# Write to file
submission.to_csv("hp_svr_rbf.csv", index=False)
# In[ ]: