forked from tuzzeg/detect_insults
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.py
243 lines (194 loc) · 5.16 KB
/
stack.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
#!/usr/bin/env python
import random
from operator import itemgetter
import math
from datetime import datetime
import numpy
from sklearn import linear_model
from sklearn import cross_validation
from sklearn import metrics
from sklearn import ensemble
from model import Basic
from mkhtml import *
from utils import *
import data
import os.path
class StackModel:
def __init__(self, n_estimators = 50, k = 4, n = 2):
self.classifierFs = [
# stem
stemLogistic,
stem12Logistic,
stem12SortedLogistic,
stemRank,
stem12Rank,
stem12SortedRank,
# words
wordsLogistic,
words2Logistic,
words12Logistic,
words12SortedLogistic,
# tags
tagsLogistic,
tags12Logistic,
tags12TfLogistic,
# word shapes
wordShapesLogistic,
wordShapesTfLogistic,
# subseq
stemsSubseq2Logistic_5,
stemsSubseq2Logistic_6,
stemsSubseq2SortedLogistic_5,
stemsSubseq2SortedLogistic_6,
stemsSubseq3Logistic_5,
stemsSubseq3Logistic_6,
stemsSubseq3SortedLogistic_5,
tagsSubseq2Logistic_5,
tagsSubseq2Logistic_6,
tagsSubseq2SortedLogistic_5,
tagsSubseq2SortedLogistic_6,
tagsSubseq3Logistic_4,
tagsSubseq3Logistic_5,
tagsSubseq3Logistic_6,
# char ngrams
ngramsLogistic_3,
ngramsLogistic_4,
ngramsTfLogistic_3,
ngramsTfLogistic_4,
ngramsRank_3,
ngramsRank_4,
ngramsTfRank_3,
ngramsTfRank_4,
ngramsLogisticSen_2,
ngramsLogisticSen_3,
ngramsLogisticSen_4,
ngramsTfLogisticSen_2,
ngramsTfLogisticSen_3,
ngramsTfLogisticSen_4,
ngramsRankSen_2,
ngramsRankSen_3,
ngramsRankSen_4,
ngramsTfRankSen_2,
ngramsTfRankSen_3,
ngramsTfRankSen_4,
# mixed ngrams
mixedST,
mixedTS,
mixedST_TS,
mixedSTT,
mixedTST,
mixedTTS,
mixedSTT_TST_TTS,
# lang models
stemLm_2,
stemLm_3,
stemLm_4,
stemLm_5,
stemLm_6,
stemLm_7,
stemLmSen_2,
stemLmSen_3,
stemLmSen_4,
stemLmSen_5,
stemLmSen_6,
stemLmSen_7,
tagLm_2,
tagLm_3,
tagLm_4,
tagLm_5,
tagLm_6,
tagLm_7,
tagLmSen_2,
tagLmSen_3,
tagLmSen_4,
tagLmSen_5,
tagLmSen_6,
tagLmSen_7,
# syntax
syntaxStems2,
syntaxStems12,
syntaxStems123,
syntaxStems2Dep,
syntaxStemsL,
syntaxStemsR,
syntaxStemsLR,
syntaxStems12_LR,
syntaxTags2,
syntaxTags3,
syntaxTags12,
syntaxTags123,
syntaxMixedST,
syntaxMixedTS,
syntaxMixedST_TS,
syntaxMixedSTT,
syntaxMixedTST,
syntaxMixedTTS,
syntaxMixedSTT_TST_TTS,
syntaxStems12Sen,
syntaxStemsRSen,
syntaxStemsLRSen,
syntaxStems12_LRSen,
# basic features
logLen,
logSentencesCount,
upperPortion,
lowerPortion,
digitPortion,
puncPortion,
otherPortion,
]
self.classifiers = None
self.estimator = ensemble.ExtraTreesRegressor(n_estimators = n_estimators, compute_importances = True, n_jobs = -1)
self.k = k
self.n = n
def train(self, rows):
Xs = []
Ys = []
rows = numpy.array(rows)
for i in range(self.n):
kfold = cross_validation.KFold(len(rows), k = self.k, indices = True, shuffle = True)
for k, (train, test) in enumerate(kfold):
rowsTrain = rows[train]
rowsTest = rows[test]
Xk = numpy.column_stack([self._train1(i, k, ci, rowsTrain, rowsTest) for ci in range(len(self.classifierFs))])
Yk = numpy.array([float(row.insult) for row in rowsTest])
Xs.append(Xk)
Ys.append(Yk)
X = numpy.vstack(Xs)
Y = numpy.concatenate(Ys)
dt = datetime.now()
print 'fit estimator, X=%s Y=%s' % (X.shape, Y.shape)
self.estimator.fit(X, Y)
print 'fit estimator, done %s' % str(datetime.now() - dt)
dt = datetime.now()
print 'final train'
self.classifiers = [f() for f in self.classifierFs]
for f, e in zip(self.classifierFs, self.classifiers):
dt1 = datetime.now()
e.train(rows)
print 'final train, %s %s' % (f.func_name, str(datetime.now() - dt1))
print 'final train, done %s' % str(datetime.now() - dt)
print 'features'
print '\n'.join(['%s\t%f' % (f.func_name, fimp) for f, fimp in zip(self.classifierFs, self.estimator.feature_importances_)])
return X, Y
def _train1(self, n, k, i, trainRows, testRows):
dt = datetime.now()
f = self.classifierFs[i]
name = f.func_name
e = f()
e.train(trainRows)
X = e.classify(testRows)
print 'trained, n=%d k=%d %s %s' % (n, k, name, str(datetime.now() - dt))
return X
def classify1(self, row):
x = self.featurize1(row)
return self.estimator.predict(x)
def classify(self, rows):
X = self.featurize(rows)
return self.estimator.predict(X)
def featurize(self, rows):
X = numpy.column_stack([e.classify(rows) for e in self.classifiers])
return X
def featurize1(self, row):
X = numpy.array([e.classify1(row) for e in self.classifiers])
return X