-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfourth_pass.py
More file actions
306 lines (262 loc) · 10.7 KB
/
Copy pathfourth_pass.py
File metadata and controls
306 lines (262 loc) · 10.7 KB
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
import pandas as pd
import numpy as np
import sys
from sklearn.cross_validation import cross_val_score, train_test_split
from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.decomposition import TruncatedSVD
from sklearn.linear_model import LarsCV
from sklearn.ensemble import GradientBoostingRegressor, RandomForestRegressor
from sklearn.cluster import MeanShift
def main():
# Pulls in all the housing data from geocoded files.
data = get_data()
# Drop a bunch of unwanted columns and parse dates.
data = normalize_data(data)
# Take categorical columns and generate dummy features with binary cels.
data = binarize_categorical_data(data)
# Split my data into the 4 sets of training and test.
data_train_x, data_test_x, data_train_y, data_test_y = split_data(data)
print "\nSize of training set: {}\nSize of testing set: {}\nNumber of features: {}\n".format(
len(data_train_y), len(data_test_y), len(data_test_x.columns.values)
)
"""
We want -
1. It's cluster derived from location, size, etc.
2. An estimate derived from remarks.
3. An estimate derived from everything but remarks.
4. An estimate derived from the previous two estimates.
"""
final_pipeline = Pipeline([
('remarks_split', FeatureUnion([
('remarks_pipe', Pipeline([
('peel_remarks', PeelRemarks()),
('fill_nans', FillNaNs()),
('tfidf', TfidfVectorizer(
ngram_range=(1, 4),
max_df=0.7,
min_df=5,
sublinear_tf=True
)),
('truncatedsvd', TruncatedSVD(
n_components=500,
algorithm='arpack'
)),
('larscv', RegressorWrapper(LarsCV(
max_iter=500,
max_n_alphas=750,
normalize=False,
cv=5,
verbose=True
)))
])),
('not_remarks_pipe', Pipeline([
('cluster', ClustererWrapper(MeanShift(
bin_seeding=True,
))),
('main_model', RegressorWrapper(GradientBoostingRegressor(
loss='huber',
n_estimators=500,
subsample=0.6,
learning_rate=0.08,
min_samples_leaf=3,
min_samples_split=1,
max_features='auto',
max_depth=5,
alpha=0.9,
min_weight_fraction_leaf=0.0,
verbose=1
)))
]))
], n_jobs=-1)),
('split_to_columns', ColumnSplitter()),
('final_model', RandomForestRegressor(
n_estimators=500,
n_jobs=-1,
verbose=1
))
])
basic_regressor = GradientBoostingRegressor(
loss='huber',
n_estimators=500,
subsample=0.6,
learning_rate=0.08,
min_samples_leaf=3,
min_samples_split=1,
max_features='auto',
max_depth=5,
alpha=0.9,
min_weight_fraction_leaf=0.0,
verbose=1
)
basic_regressor.fit(data_train_x.drop('REMARKS', 1).fillna(0), data_train_y)
report_accuracy(basic_regressor, data_test_x.drop('REMARKS', 1).fillna(0), data_test_y, name='simple')
final_pipeline.fit(data_train_x, data_train_y)
report_accuracy(final_pipeline, data_test_x, data_test_y, name='best')
import ipdb; ipdb.set_trace()
def get_data():
# Read the first argument, csv -> DataFrame
data = pd.read_csv(
sys.argv[1],
index_col="MLSNUM",
parse_dates=["LISTDATE", "SOLDDATE", "EXPIREDDATE"],
encoding="utf-8"
)
if len(sys.argv) > 2:
# Read subsequent arguments, appending them into the same DataFrame.
for f in sys.argv[2:]:
new_data = pd.read_csv(
f,
index_col="MLSNUM",
parse_dates=["LISTDATE", "SOLDDATE", "EXPIREDDATE"],
encoding="utf-8"
)
data = data.append(new_data)
return data
def normalize_data(data):
# Drop all the columns that we don't want.
data = data.drop(['Unnamed: 0', 'EXPIREDDATE', 'COOLING', 'AREA', "SHOWINGINSTRUCTIONS", "OFFICEPHONE", "STATUS", "OFFICENAME", "HOUSENUM2", "HOUSENUM1",
"DTO", "DOM", "JUNIORHIGHSCHOOL", "AGENTNAME", "HIGHSCHOOL", "STREETNAME", "PHOTOURL", "HIGHSCHOOL", "ELEMENTARYSCHOOL", "ADDRESS", "LISTPRICE"], 1)
# Convert dates into number of days since the latest date.
for x in ["LISTDATE", "SOLDDATE"]:
data[x] = (
data[x] - data[x].min()).astype('timedelta64[M]').astype(int)
return data
def binarize_categorical_data(data):
# Column 'OTHERFEATURES' contains a semicolon seperated
# string of feature:value pairs. We need to parse those for
# every row and seperate them into their own columns.
sub_columns = ['Basement', 'Fireplaces', 'Roof', 'Floor', 'Appliances', 'Foundation', 'Construction',
'Exterior', 'Exterior Features', 'Insulation', 'Electric', 'Interior Features', 'Hot Water']
for sub_column in sub_columns:
data[sub_column] = data['OTHERFEATURES'].str.extract(
"{}:(.*?);".format(sub_column))
data = data.drop('OTHERFEATURES', 1)
# Take these unstandardized fields and create 'dummy columns' from them
# which have a 1 or 0 for each row. The number of dummy columns is equal
# to the number of distinct possible answers for each column.
#
# i.e. PROPTYPE will get split up into (PROPTYPE) SF and (PROPTYPE) MF
sub_columns.extend(
["PROPTYPE", "STYLE", "HEATING", "CITY", "LEVEL", "STATE"])
for var in sub_columns:
if var == "LEVEL":
# let the hate flow through you young padawan.
data[var] = data[var].fillna(0.0).replace(
to_replace='B', value=1.0).astype(int).astype(str)
else:
# Calling .lower() on an int makes it None.
data[var] = data[var].str.lower()
new_data = data[var].str.get_dummies(sep=', ')
new_data.rename(
columns=lambda x: "({}) ".format(var) + x, inplace=True)
data = pd.concat([data, new_data], axis=1, join='inner')
data = data.drop(var, 1)
return data
def split_data(data):
from sklearn.cross_validation import train_test_split
x = data.drop('SOLDPRICE', 1)
y = data['SOLDPRICE']
return train_test_split(x, y, test_size=0.25)
def report(grid_scores, n_top=3):
from operator import itemgetter
top_scores = sorted(grid_scores, key=itemgetter(1), reverse=True)[:n_top]
for i, score in enumerate(top_scores):
print("Model with rank: {0}".format(i + 1))
print("Mean validation score: {0:.3f} (std: {1:.3f})".format(
score.mean_validation_score,
np.std(score.cv_validation_scores)))
print("Parameters: {0}".format(score.parameters))
print("")
def report_accuracy(model, data_test_x, data_test_y, name="model"):
print "{:-^60}".format(name.upper() + " ACCURACY")
score = model.score(data_test_x, data_test_y)
print "MSE Accuracy: {}".format(score)
data_predicted_y = model.predict(data_test_x)
print "MAPE: {}".format(mean_absolute_percentage_error(data_test_y, data_predicted_y))
sample_predictions(model.predict(data_test_x), data_test_y)
cross_validated_scores = cross_val_score(
model, data_test_x, data_test_y, cv=5)
print "MSE Across 5 Folds: {}".format(cross_validated_scores)
print "95%% Confidence Interval: %0.3f (+/- %0.3f)\n" % (cross_validated_scores.mean(), cross_validated_scores.std() * 1.96)
def mean_absolute_percentage_error(y_true, y_pred):
return np.mean(np.abs((y_true - y_pred) / y_true))
def sample_predictions(predicted, actual):
sample_size = 25
samples = np.random.randint(0, high=len(actual), size=sample_size)
print '{:^30}'.format("Predicted"),
print '{:^30}\n'.format("Actual")
for sample in samples:
print '{:^30,}'.format(int(predicted[sample])),
print '{:^30,}'.format(int(actual.iloc[[sample]].values[0]))
class PeelRemarks:
# Input: Full data block.
# Fit: Pass.
# Transform: Return only remarks column.
def fit(self, x, y):
print "Fitting PeelRemarks"
return self
def transform(self, x):
return x['REMARKS']
class FillNaNs:
# Input: Remarks column.
# Fit: Pass.
# Transform: Remarks column with all np.nan's replaced.
def fit(self, x, y):
print "Fitting FillNaNs"
return self
def transform(self, x):
return x.fillna('')
class ClustererWrapper:
# Input: Full data block.
# Fit: Train cluster alg on subset of data.
# Transform: Predict based on subset. Append predictions to data.
# Peel off remarks. Return data.
# data.
def __init__(self, model):
self.cluster_model = model
def fit(self, x, y):
print "Fitting ClustererWrapper"
subset = self.get_subset(x)
self.cluster_model.fit(subset, y)
return self
def transform(self, x):
subset = self.get_subset(x)
x['cluster'] = self.cluster_model.predict(subset)
return x.drop('REMARKS', 1).fillna(0)
def get_subset(self, x):
# 'AGE'?
return x[['lat', 'lng', 'SQFT', 'BEDS', 'BATHS', 'ZIP', 'GARAGE', 'LOTSIZE']].fillna(0)
def get_params(self, deep=True):
return self.cluster_model.get_params(deep=deep)
def set_params(self, params):
return self.cluster_model.set_params(params)
class RegressorWrapper:
# Input: Output from TruncatedSVD
# Fit: Fit the internal model.
# Transform: Return predictions only.
def __init__(self, model):
self.regressor_model = model
def fit(self, x, y):
print "Fitting RegressorWrapper"
self.regressor_model.fit(x, y)
return self
def transform(self, x):
return self.regressor_model.predict(x)
class ColumnSplitter:
# I'm upset about the necessity of this class. FeatureUnion is appending rows and not columns but we need columns.
# Input: (2*X, 1) array of predictions.
# Fit: Pass.
# Transform: Transform into (X, 2). Predict.
def fit(self, x, y):
print "Fitting ColumnSplitter"
return self
def transform(self, x):
s1 = pd.Series(x[:len(x)/2])
s2 = pd.Series(x[len(x)/2:])
return pd.concat([s1, s2], axis=1)
if __name__ == "__main__":
if len(sys.argv) > 1:
main()
else:
print "Error: Missing input file arguments."