/
train.py
182 lines (146 loc) · 5.9 KB
/
train.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
#!/usr/local/bin/python
# coding: utf-8
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.metrics import median_absolute_error
from sklearn.metrics import mean_squared_error
from sklearn.metrics import explained_variance_score
from sklearn.metrics import classification_report
import time
import logging
import sys
import numpy as np
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
LOG = logging.getLogger('training')
def _descritize(y, scale, lower = 5, upper = 3000):
y = np.asarray(y)
y = np.clip(scale * np.round(y/scale, 1), lower, upper).astype(int)
y = ["%d" % cls for cls in y]
return y
class Trainer(object):
def __init__(self, x, y, train_ratio):
sample_train = int(train_ratio * len(x))
self._x_train = x[:sample_train]
self._y_train = y[:sample_train]
self._x_test = x[len(x) - sample_train:]
self._y_test = y[len(y) - sample_train:]
self._trained = False
self._model = None
def Train(self):
self.Fit()
x_train = self.Preprocess(self._x_train)
self._model = self.Learn(x_train, self._y_train)
self._trained = True
def Predict(self, x):
if not self._trained:
raise RuntimeError(
'Prediction requested but model is not trained yet.')
x_test = self.Preprocess(x)
return self._model.predict(x_test)
def PredictProba(self, x):
if not self._trained:
raise RuntimeError(
'Prediction requested but model is not trained yet.')
x_test = self.Preprocess(x)
return self._model.predict_proba(x_test)
def Report(self):
if not self._trained:
raise RuntimeError(
'Report requested but model is not trained yet.')
report = self.Eval()
for k, v in report.iteritems():
LOG.info('metric [%s] = %s', k, str(v))
def Preprocess(self, x):
return x
def Fit(self):
pass
def Learn(self, x_train, y_train):
raise NotImplementedError(
'Learn() is not implemented by Trainer subclass.')
def Eval(self):
raise NotImplementedError(
'Eval() is not implemented by Trainer subclass.')
class CaloriesRegressor(Trainer):
def __init__(self, x, y, train_ratio):
super(CaloriesRegressor, self).__init__(x, y, train_ratio)
self._count_vec = CountVectorizer()
self._tfidf_transformer = TfidfTransformer()
def Fit(self):
x_count = self._count_vec.fit_transform(self._x_train)
self._tfidf_transformer.fit(x_count)
def Preprocess(self, x):
return self._tfidf_transformer.transform(self._count_vec.transform(x))
def Learn(self, x_train, y_train):
LOG.info('x_train.shape = %s', str(x_train.shape))
LOG.info('len(y_train) = %d', len(y_train))
clf = RandomForestRegressor(verbose=0, n_jobs=-1, n_estimators=100)
LOG.info('Training...')
clf.fit(x_train, y_train)
LOG.info('Done...')
return clf
def Eval(self):
LOG.info('Eval ...')
y_pred = self.Predict(self._x_test)
return {
'median_absolute_error':
median_absolute_error(self._y_test, y_pred),
'mean_squared_error': mean_squared_error(self._y_test, y_pred),
'explained_variance_score':
explained_variance_score(self._y_test, y_pred),
}
class CaloriesClassifier(Trainer):
def __init__(self, x, y, train_ratio):
super(CaloriesClassifier, self).__init__(x, y, train_ratio)
self._count_vec = CountVectorizer()
self._tfidf_transformer = TfidfTransformer()
self._y_train = _descritize(self._y_train, 100)
self._y_test = _descritize(self._y_test, 100)
def Fit(self):
x_count = self._count_vec.fit_transform(self._x_train)
self._tfidf_transformer.fit(x_count)
def Preprocess(self, x):
print x[0]
return self._tfidf_transformer.transform(self._count_vec.transform(x))
def Learn(self, x_train, y_train):
LOG.info('x_train.shape = %s', str(x_train.shape))
LOG.info('len(y_train) = %d', len(y_train))
clf = RandomForestClassifier(verbose=0, n_jobs=-1, n_estimators=10)
LOG.info('Training...')
clf.fit(x_train, y_train)
LOG.info('Done...')
return clf
def Eval(self):
LOG.info('Eval ...')
y_pred = self.Predict(self._x_test)
return {
'misclass': np.mean(y_pred != self._y_test),
'report': classification_report(self._y_test, y_pred,
target_names=self._model.classes_)
}
class UnitClassifier(Trainer):
def __init__(self, x, y, train_ratio):
super(UnitClassifier, self).__init__(x, y, train_ratio)
self._count_vec = CountVectorizer()
self._tfidf_transformer = TfidfTransformer()
def Fit(self):
x_count = self._count_vec.fit_transform(self._x_train)
self._tfidf_transformer.fit(x_count)
def Preprocess(self, x):
return self._tfidf_transformer.transform(self._count_vec.transform(x))
def Learn(self, x_train, y_train):
LOG.info('x_train.shape = %s', str(x_train.shape))
LOG.info('len(y_train) = %d', len(y_train))
clf = RandomForestClassifier(verbose=0, n_jobs=-1, n_estimators=20)
LOG.info('Training...')
clf.fit(x_train, y_train)
LOG.info('Done...')
return clf
def Eval(self):
LOG.info('Eval ...')
y_pred = self.Predict(self._x_test)
return {
'misclass': np.mean(y_pred != self._y_test),
'report': classification_report(self._y_test, y_pred,
target_names=self._model.classes_)
}