-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·153 lines (131 loc) · 4.38 KB
/
main.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
#!/usr/bin/python
import numpy as np
import pandas as pd
from utils.playsound_tobi import *
from utils.loader import *
import sys
import time
####################
# load and preprocess data
####################
l = Loader("./data/train.csv")
df, features = l.process(do_filter=True)
l_test = Loader("./data/test.csv")
df_test, features_test = l_test.process()
####################
# split data
####################
from sklearn.cross_validation import train_test_split
#df_y = np.array(df.Category.astype('category').cat.codes.astype('float32'))
df_y = np.array(df.Category)#.astype('category')
df_x = np.array(df[features]).astype('float32')
# prepare test set
df_x_test = np.array(df_test[features]).astype('float32')
from sklearn.utils import shuffle
df_x_shuf, df_y_shuf = shuffle(df_x, df_y, random_state=0)
_submission = False
if not _submission:
Y_test = []
Y = [1,2,3]
# log_loss only works when all categories are present in dataset
# with too small train/test sizes, sometimes a lable is not present in either group
while len(np.unique(Y_test)) != len(np.unique(Y)):
print "Splitting dataset"
train, test, Y, Y_test = train_test_split(df_x_shuf, df_y_shuf, train_size=0.9, random_state=1337)
len(train)
len(np.unique(Y))
else:
train = df_x_shuf
Y = df_y_shuf
test = df_x_test
####################
# train a classifier
####################
# T = Tree
# F = Forest
# X = xgboost
# A = adaboost
for classifier in ['A','T','F']:
if classifier == 'T':
print "Training Decision Tree"
from sklearn import tree
# parameters obtained by random search
clf = tree.DecisionTreeClassifier(
max_depth=8,
min_samples_leaf=1,
random_state=1337)
clf.fit(train, Y)
elif classifier == 'F':
print "Training Random Forest"
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(
max_depth=20,
n_estimators=512,
bootstrap=False,
n_jobs=60,
random_state=1337)
elif classifier == 'X':
print "Training xgboost"
import xgboost as xgb
clf = xgb.XGBClassifier(
max_depth=20,
n_estimators=512,
learning_rate=0.05,
)
elif classifier == 'A':
print "Training Adaboost"
from sklearn.ensemble import AdaBoostClassifier
clf = AdaBoostClassifier(
learning_rate=0.2,
algorithm="SAMME.R",
n_estimators=10,
random_state=1337)
st = time.time()
clf.fit(train, Y)
time_taken = time.time() - st
print "Took: ", time_taken
print "Predicting %d samples" % (len(test))
st = time.time()
preds = clf.predict_proba(test)
print "Took: ", time.time() - st
if not _submission:
from sklearn.metrics import log_loss
ll = log_loss(Y_test, preds)
print "LogLoss: %f" % ll
# write result to file for later comparison
import datetime
now = datetime.datetime.now()
fname = "log_experiment_1.txt"
if classifier == 'T':
clfname = "DecisionTree"
#fname = "log_tree.txt"
elif classifier == 'F':
clfname = "RandomForest"
#fname = "log_forest.txt"
elif classifier == 'X':
clfname = "XGBoost"
#fname = "log_xgboost.txt"
elif classifier == 'A':
clfname = "Adaboost"
#fname = "log_adaboost.txt"
f = open(fname, "a+")
f.write("%02d.%02d.%d %02d:%02d == %s ======================================\r\n" % (now.day, now.month, now.year, now.hour, now.minute, clfname))
for k,v in clf.get_params().iteritems():
f.write("%s:\t " % k)
f.write(str(v))
f.write("\r\n")
f.write("Features: ")
for feat in features:
f.write("%s, " % feat)
f.write("\r\n")
f.write("LogLoss: %f\r\n" % ll)
f.write("Time taken: %f\r\n" % (time_taken) )
f.close()
else:
#Write results
result=pd.DataFrame(preds, columns=clf.classes_)
result.to_csv('testResult.csv', index = True, index_label = 'Id' )
# disabled, does not work on server
# play a sound when done!
#Playsound('./utils/complete.wav')
print "Done."