-
Notifications
You must be signed in to change notification settings - Fork 0
/
otto_Pikki.py
464 lines (354 loc) · 13.1 KB
/
otto_Pikki.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#%%writefile otto_Pikki.py
#%matplotlib inline
import pandas as pd
import numpy as np
from sklearn.utils import shuffle
#from sklearn.ensemble import RandomForestClassifier ,GradientBoostingClassifier
#from sklearn.feature_selection import SelectKBest
#from sklearn.feature_selection import chi2, f_classif
#from sklearn.tree import DecisionTreeClassifier
#from sklearn.pipeline import Pipeline
#from sklearn.lda import LDA
#from sklearn.neural_network import BernoulliRBM
#from sklearn.svm import LinearSVC
from sklearn import cross_validation
#from sklearn.linear_model import (RandomizedLasso, lasso_stability_path,
# LassoLarsCV)
from matplotlib import pyplot as plt
def sigmoid(x):
print('XXXX')
y = 1/(1+np.exp(x))
return y
import time
def load_data(train=True):
"""loads Data.
train == True means training data which has y values
train == False means testing (=pleasePredict) data, which only has X values
"""
#test set
start = time.clock()
if train==False:
#dp = '/home/thomas/Desktop/OttoChallenge/test.csv'
dp ='test.csv'
df = pd.read_csv(dp)
X = df.values.astype(np.float32)[:,1:]
return X
#train set
else:
#dp = '/home/thomas/Desktop/OttoChallenge/train.csv'
dp = 'train.csv'
df = pd.read_csv(dp)
X = df[df.columns[:-1]].values.astype(np.float32)[:,1:]
y = df.target
y =y.apply(lambda X: int(X[-1])).values
y = y.astype(np.int32)
X, y = shuffle(X, y)
#print(X.shape,y.shape)
end = time.clock()
print(end-start)
return X,y
def make_dummies(df,df_2):
num_train = df.shape[0]
df['train'] = 1
df_2['train'] = 0
df_2.index += num_train
df = df.append(df_2)
var_list = list(df.columns)
for name in var_list:
df_tmp = pd.get_dummies(df[name],prefix=name)
df.drop(name,axis=1)
df = df.join(df_tmp)
df_train = df[df['train']==1]
df_test = df[df['train']==0]
X = df_train.values.astype(np.float32)
X_test = df_test.values.astype(np.float32)
return X,X_test
def generate_features(X, shuffle_data = False, which_slice = 0,how_many_slices = 1,dummies = False):
"""generates features from np.array X.
most of them aren't that great but give a little boost in performance. Last 93 features are interesting,
computed from a correlation matrix.
shuffle Data shuffles the data,
how many slices specifies by what number the data should be divided
which slice then states which part of the data should be used (mind the 0)
dummes keyword doesn't work yet.
"""
start = time.clock()
df_X = pd.DataFrame(X)
if dummies ==True:
print('dummies')
df_tmp = df_X.cumsum(axis=1)
df_tmp_2 = df_X.cumprod(axis=1)
df_tmp_5 = df_X.cummax(axis=1)
df_tmp_6 = df_X.cummin(axis=1)
df_tmp_3 = df_X**2
df_tmp_4 = df_X.T.diff(2).T.dropna(axis=1,how='any')
#df_X**3
df_tmp_7 = df_X.T.diff(1).T.dropna(axis=1,how='any')
df_tmp_8 = df_X**3
df_tmp_9 = df_X**5
df_tmp_10 = df_X - df_X.apply(np.mean,axis=0)#will this work?
df_test = pd.stats.moments.rolling_mean(df_X.T,window = 19,axis=1)
df_test = df_test.T.dropna(axis=1,how='any')
df_X['nonzero_sum'] = df_X.apply(np.count_nonzero,axis=1)
# df_X['binc'] = df_X.apply(np.unique,axis=1)
corr = df_X.corr()
df_tmp_11 = df_X.dot(corr)
df_X['mean'] = df_X.apply(np.mean,axis=1,raw=True)
df_X['std'] = df_X.apply(np.std,axis=1,raw=True)
df_X['median'] = df_X.apply(np.median,axis=1,raw=True)
df_X['amax'] = df_X.apply(np.amax,axis=1,raw=True)
df_X['amin'] = df_X.apply(np.amin,axis=1,raw=True)
df_X['ptp'] = df_X.apply(np.ptp,axis=1)
df_X = pd.concat([df_X,
df_tmp,
df_tmp_2,
#df_tmp_3,
df_tmp_4,
df_tmp_5,
df_tmp_6,
#df_tmp_7,
#df_tmp_8,
#df_tmp_9,
df_tmp_11,
df_test
],axis=1)
X = df_X.values.astype(np.float32)
if shuffle_data == True:
z = shuffle(X.T)
X = z.T
print('shape')
print(X.shape)
slice_length = round(X.shape[1]/how_many_slices)
print(slice_length, how_many_slices)
X = X[:,which_slice*slice_length:(which_slice+1)*slice_length]
print('shape new')
print(X.shape)
end = time.clock()
return X
from sklearn.metrics import log_loss
def get_score(clf, features = True,which_slice = 0,how_many_slices = 1,shuffle_data = False):
"""computes a cross validation score
takes all keywords from generate_features"""
start = time.clock()
X,y = load_data()
if features == True:
X = generate_features(X,which_slice=which_slice,how_many_slices=how_many_slices ,shuffle_data=shuffle_data)
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X,y, test_size=0.2)
#print(y_train)
clf.fit(X_train,y_train)
outcome = clf.predict_proba(X_test)
end = time.clock()
print('SCORE: ' , log_loss(y_test,outcome), ' time: ',end-start)
def get_score2(clf,features = True,which_slice = 1,how_many_slices = 5,shuffle_data = False):
X,y = load_data()
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X,y, test_size=0.2)#, random_state=0)
clf.fit(X_train,y_train)
outcome = clf.predict_proba(X_test)
print(log_loss(y_test,outcome))
def make_guess_csv(clf,features =True,name = 'test',which_slice = 0,how_many_slices = 1,shuffle_data = False):
"""generates a csv file containing the output from one classifier.
index starts at 0, so no submittable file is created (was necessary in an older version of combine_results,
not sure if it still is)
"""
start = time.clock()
X_test,y_test = load_data()
X = load_data(train=False)
if features == True:
X = generate_features(X,which_slice=which_slice ,how_many_slices=how_many_slices ,shuffle_data=shuffle_data)
X_test = generate_features(X_test,which_slice=which_slice,how_many_slices=how_many_slices ,shuffle_data=shuffle_data )
clf.fit(X_test,y_test)
output = clf.predict_proba(X)
cols = ['Class_1',
'Class_2',
'Class_3',
'Class_4',
'Class_5',
'Class_6',
'Class_7',
'Class_8',
'Class_9']
df_write = pd.DataFrame(output)
shuf = '_noshuf_'
if shuffle_data==True:
shuf = '_shufled_'
slices = 'num_slice_' + str(how_many_slices)
which_clf = clf
num_slice = 'slice_num_' + str(which_slice)
clf = str(clf)
file_name = 'Pikki' + name +num_slice + slices + shuf + '.csv'
print('written to ' + file_name)
df_write.to_csv(file_name,header = cols,index_label = ['id'])
end = time.clock()
print(end-start)
identifier = name +num_slice + slices + shuf
return identifier
def load_all_dfs(clf_list = ['test_small','test2_small']):#,'rt_small']):
"""loads all the csv files that end with specification in clf_list
in multiindex DataFrame. first row is specification, second is index
helper for combine_results and plot_bias
"""
start = time.clock()
print('loading data')
first_clf = clf_list[0]
df = pd.read_csv('Pikki'+first_clf+'.csv')
df['df'] = first_clf
df = df.set_index(['id','df'])
for clf in clf_list[1:]:
file_name = 'Pikki' + clf + '.csv'
df_tmp = pd.read_csv(file_name)
df_tmp['df'] = clf
df_tmp = df_tmp.set_index(['id','df'])
df = pd.concat([df,df_tmp])
df['std'] = df.apply(np.std,axis=1,raw = True)
end = time.clock()
print(end-start)
return df#.swaplevel(0,1)
def combine_results(voting = 'hard',clf_list = ['test_small','test2_small']):
"""combines two or more classifier outputs.
by now only uses hard voting by standard deviation
(more std means higher values which ~ means higher confidence)
other measures welcome
creates both a submittable file ("combined ...")
and a file that can be used for combination with other classifiers
"""
start = time.clock()
df = load_all_dfs(clf_list)
print('combining the data and voting ', voting)
if voting == 'hard':
print('voting')
label_tupel_list = list(df.groupby(level=['id'])['std'].idxmax())#idmax
num_samples = len(label_tupel_list)
index = [label_tupel_list[i][0] for i in range(num_samples)]
df.index
time_need = []
t2 = 0
print("doing god's work")
df_new = df.ix[index]
df_new = df.ix[label_tupel_list]
#### new###
if voting == 'soft':
clsfr1 = clf_list[0]
clsfr2 = clf_list[1]
print('softvoting')
df = df.reset_index()
df1 = df[df.df==clsfr1]
df2 = df[df.df==clsfr2]
std1 = df1['std']
std2 = df2['std']
df1 = df1.drop('id',axis=1)
df2 = df2.drop('id',axis=1)
df1 = df1.drop('df',axis=1)
df2 = df2.drop('df',axis=1)
df1 = df1.drop('std',axis=1)
df2 = df2.drop('std',axis=1)
df1_new = df1.pow(std1.values,axis = 0)#pd.DataFrame(df1.values**std1.values, columns = df1.columns, index=df1.index)
df2_new = df2.pow(std2.values,axis=0)#pd.DataFrame(df2.values**std2.values, columns = df2.columns, index=df2.index)
df_new = pd.DataFrame(df1_new.values*df2_new.values, columns=df1.columns, index=df1.index)
df_new = df_new.div(df_new.sum(axis=1), axis=0)
####endnew
end = time.clock()
print('done', end-start)
#return df_new
cols = ['Class_1',
'Class_2',
'Class_3',
'Class_4',
'Class_5',
'Class_6',
'Class_7',
'Class_8',
'Class_9']
try:
df_new2 = df_new.reset_index()
del df_new2['std']
del df_new2['id']
del df_new2['df']
except:
df_new2 = df_new
print('zero')
try:
print('first')
clf_names = voting +'_'
print('second')
for i in range(len(clf_list)):
print(clf_list[i])
clf_names = clf_names + '_' + clf_list[i]
df_new2.to_csv('Pikki'+clf_names+ '.csv',header = cols,index_label = ['id'])
df_new2.index +=1
print('written to')
print('Pikki'+clf_names+ '.csv')
name = 'combined_Pikki'+clf_names+ '.csv'
df_new2.to_csv(name,header = cols,index_label = ['id'])
except:
df_new2.to_csv('combined_Pikki.csv',header = cols,index_label = ['id'])
return df_new
def vote(clf_list=['test_small','test2_small']):
a = combine_results(clf_list=clf_list,voting='soft')
#clf_list2 =
clf_list.append('soft__'+clf_list[0]+'_'+clf_list[1])
b = combine_results(clf_list=clf_list,voting='hard')
def plot_bias(clf_list = ['test_small','rt_small','test2_small'],return_df = False,XKCD = False):
"""plots some differences between two output files.
right now plots for every class and every classifier
mean
max
std
diff(clfx-clfy)
plt.xkcd() is optional.
"""
if XKCD == True:
plt.xkcd()
print('damn')
df = load_all_dfs(clf_list)
df = df.swaplevel(0,1)
del df['std']
df.hist()
plt.figure()
for clf in clf_list:
df.ix[clf].mean().plot(label = clf,figsize=(16, 4))
plt.legend(loc='upper right')
plt.title('mean')
plt.figure()
# c = df.columns
for clf in clf_list:
#df[c[1:]].ix[clf].max().plot(label = clf,figsize=(16, 4))
df.ix[clf].max().plot(label = clf,figsize=(16, 4))
plt.legend(loc='upper right')
plt.title('max')
plt.figure()
for clf in clf_list:
df.ix[clf].std().plot(label = clf,figsize=(16, 4))
plt.legend(loc='upper right')
plt.title('std')
plt.figure()
used_list = []
for clf in clf_list:
for clf2 in clf_list:
if (clf != clf2) and ({clf,clf2} not in used_list):
diff = ((df.ix[clf] - df.ix[clf2])**2)**(1/2)
diff.mean().plot(label = clf+' - ' +clf2,figsize=(16, 4))
used_list.append({clf,clf2})
plt.legend(loc='upper right')
plt.title('difference')
print('damnover')
if return_df == True:
return df
def example():
"""example on how to run this"""
#generate a classifier
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
clf = RandomForestClassifier(n_estimators=4,n_jobs=-1)
clf2 = GradientBoostingClassifier(n_estimators = 1)
#make guess csv
n1 = make_guess_csv(clf,name='example')
n2 = make_guess_csv(clf2,name='example_2')
#combine results
print(n1,n2)
combine_results(clf_list=[n1,n2])
#plot the stuff
plot_bias([n1,n2],XKCD = True)
#noclue if this is necessary. If somebody has better information, please let me know
#def main():
# example()
#if __name__ == "__main__":
# main()