This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcombine_new.py
176 lines (159 loc) · 7.48 KB
/
combine_new.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
from data_io import DataIO
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.cross_validation import cross_val_score
import numpy as np
from sklearn.metrics import mean_absolute_error
from itertools import combinations
from sklearn.linear_model import LinearRegression, RidgeCV, Ridge
dio = DataIO("Settings.json")
submission = False
n_trees = 10
min_samples_split = 2
param = """Normal count vector with max 200. New submission which is repeatable.
and nicer
"""
if submission:
type_n = "train_full"
type_v = "valid_full"
else:
type_n = "train"
type_v = "valid"
#columns = ["Category", "ContractTime", "ContractType"]
#le_features = dio.get_le_features(columns, "train_full")
#extra_features = dio.get_features(columns, type_n, le_features)
#extra_valid_features = dio.get_features(columns, type_v, le_features)
#features = dio.join_features("%s_" + type_n + "_tfidf_matrix_max_f_200",
#["Title", "FullDescription", "LocationRaw"],
#extra_features)
#validation_features = dio.join_features("%s_" + type_v + "_tfidf_matrix_max_f_200",
#["Title", "FullDescription", "LocationRaw"],
#extra_valid_features)
#print "features", features.shape
#print "valid features", validation_features.shape
#salaries = dio.get_salaries(type_n, log=True)
if not submission:
valid_salaries = dio.get_salaries(type_v, log=False)
print valid_salaries.shape
model_names = []
for n_trees in [20, 30, 40]:
name = "ExtraTree_min_sample2_%dtrees_200f_noNorm_categoryTimeType_new_log" % (n_trees)
model_names.append(name)
name = "ExtraTree_min_sample2_%dtrees_200f_noNorm_categoryTimeType_new" % (n_trees)
model_names.append(name)
name = "ExtraTree_min_sample2_%dtrees_200f_noNorm_categoryTimeType_tfidfl2_new_log" % (n_trees)
model_names.append(name)
model_names.append("vowpall")
model_names.append("vowpall_loc5")
#fit_predict(model2)
#fit_predict(model1)
#fit_predict(model3)
#fit_predict(model6, "", "", "")
all_model_predictions = []
for model_name in model_names:
model_predictions = dio.get_prediction(model_name=model_name, type_n=type_v)
#print model_predictions[0]
if not model_name.endswith("log") and not model_name.startswith("vowpall"):
model_predictions = np.log(model_predictions)
#if model_name.startswith("vowpall"):
#model_predictions = np.log(model_predictions)
#print model_predictions[0]
print "%s\nMAE: %f\n" % (model_name, mean_absolute_error(valid_salaries, np.exp(model_predictions)))
all_model_predictions.append(model_predictions)
predictions = np.vstack(all_model_predictions).T
predictions = np.exp(predictions)
#predictions = np.random.randint(0,5, size=(10,3))
print predictions.shape
print predictions[1:10, :]
indexes = range(0, len(model_names))
def print_index(index):
names = map(lambda x: model_names[x], index)
return "\n ".join(names)
best_average = (10000, (0))
best_classifier = best_average
for num in range(2, len(model_names) + 1):
for average_index in combinations(indexes, num):
#print average_index
#print print_index(average_index)
my_prediction = predictions[:, average_index]
#print my_prediction[1:10,:]
mean_pred = my_prediction.mean(axis=1)
mae = mean_absolute_error(mean_pred, valid_salaries)
#print "MAE:", mae
if best_average[0] > mae:
best_average = (mae, average_index)
classifier = LinearRegression()
#classifier = RidgeCV(loss_func=mean_absolute_error)
#classifier.fit(my_prediction, valid_salaries)
#alpha = classifier.alpha_
#classifier = Ridge(alpha=alpha)
scores = cross_val_score(classifier, my_prediction, valid_salaries, cv=5, score_func=mean_absolute_error, verbose=0, n_jobs=-1)
#print "Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() / 2)
if best_classifier[0] > scores.mean():
best_classifier = (scores.mean(), average_index)
print "best average:", best_average[0], "\n", print_index(best_average[1])
print "best classifier:", best_classifier[0], "\n", print_index(best_classifier[1])
#
#(0, 1)
#ExtraTree_min_sample2_20trees_200f_noNorm_categoryTimeType_log vowpall
#MAE: 5925.75752661
#(0, 2)
#ExtraTree_min_sample2_20trees_200f_noNorm_categoryTimeType_log Random_forest_min_sample2_20trees_200f_noNorm_categoryTimeType_log
#MAE: 6373.82572206
#(1, 2)
#vowpall Random_forest_min_sample2_20trees_200f_noNorm_categoryTimeType_log
#MAE: 6157.26021497
#(0, 1, 2)
#ExtraTree_min_sample2_20trees_200f_noNorm_categoryTimeType_log vowpall
#MAE: 5889.15272898
#Linear regression (0,1,2):
#Accuracy: 5834.14 (+/- 29.86)
#Ridge Linear je isto
#best average: 5766.06198285 ExtraTree_min_sample2_20trees_200f_noNorm_categoryTimeType_log, vowpall, ExtraTree_min_sample2_40trees_200f_noNorm_categoryTimeType_log
#best classifier: 5778.35931012 vowpall, ExtraTree_min_sample2_40trees_200f_noNorm_categoryTimeType_log
#best average: 5766.06198285 ExtraTree_min_sample2_20trees_200f_noNorm_categoryTimeType_log, vowpall, ExtraTree_min_sample2_40trees_200f_noNorm_categoryTimeType_log
#best classifier: 5778.35931012 vowpall, ExtraTree_min_sample2_40trees_200f_noNorm_categoryTimeType_log
#best average: 5694.24220595 ExtraTree_min_sample2_20trees_200f_noNorm_categoryTimeType_log
#vowpall
#vowpall_loc5
#ExtraTree_min_sample2_40trees_200f_noNorm_categoryTimeType
#ExtraTree_min_sample2_40trees_200f_noNorm_categoryTimeType_tfidf_log
#ExtraTree_min_sample2_40trees_200f_noNorm_categoryTimeType_tfidf1_log
#best classifier: 5691.47252485 ExtraTree_min_sample2_20trees_200f_noNorm_categoryTimeType_log
#vowpall
#Random_forest_min_sample2_20trees_200f_noNorm_categoryTimeType_log
#Random_forest_min_sample2_40trees_200f_noNorm_categoryTimeType_log
#vowpall_loc5
#ExtraTree_min_sample2_40trees_200f_noNorm_categoryTimeType
#ExtraTree_min_sample2_40trees_200f_noNorm_categoryTimeType_tfidf_log
#ExtraTree_min_sample2_40trees_200f_noNorm_categoryTimeType_tfidf1_log
#best average: 5713.96940131
#ExtraTree_min_sample2_20trees_200f_noNorm_categoryTimeType_new_log
#ExtraTree_min_sample2_30trees_200f_noNorm_categoryTimeType_new_log
#ExtraTree_min_sample2_40trees_200f_noNorm_categoryTimeType_new_log
#ExtraTree_min_sample2_40trees_200f_noNorm_categoryTimeType_new
#vowpall
#vowpall_loc5
#best classifier: 5715.27766624
#ExtraTree_min_sample2_20trees_200f_noNorm_categoryTimeType_new
#ExtraTree_min_sample2_30trees_200f_noNorm_categoryTimeType_new_log
#ExtraTree_min_sample2_30trees_200f_noNorm_categoryTimeType_new
#ExtraTree_min_sample2_40trees_200f_noNorm_categoryTimeType_new_log
#ExtraTree_min_sample2_40trees_200f_noNorm_categoryTimeType_new
#vowpall
#vowpall_loc5
#best average: 5699.8652553
#ExtraTree_min_sample2_30trees_200f_noNorm_categoryTimeType_new_log
#ExtraTree_min_sample2_40trees_200f_noNorm_categoryTimeType_new_log
#ExtraTree_min_sample2_40trees_200f_noNorm_categoryTimeType_new
#ExtraTree_min_sample2_40trees_200f_noNorm_categoryTimeType_tfidfl2_new_log
#vowpall
#vowpall_loc5
#best classifier: 5700.57319137
#ExtraTree_min_sample2_20trees_200f_noNorm_categoryTimeType_tfidfl2_new_log
#ExtraTree_min_sample2_30trees_200f_noNorm_categoryTimeType_new_log
#ExtraTree_min_sample2_30trees_200f_noNorm_categoryTimeType_new
#ExtraTree_min_sample2_30trees_200f_noNorm_categoryTimeType_tfidfl2_new_log
#ExtraTree_min_sample2_40trees_200f_noNorm_categoryTimeType_new_log
#ExtraTree_min_sample2_40trees_200f_noNorm_categoryTimeType_new
#vowpall
#vowpall_loc5