-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstandard_template.py
549 lines (451 loc) · 18.3 KB
/
standard_template.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# -*- coding: utf-8 -*-
import numpy as np
import warnings
from sklearn import preprocessing
from sklearn.svm import SVC
from sklearn import linear_model
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import MultinomialNB
from sklearn.ensemble import RandomForestClassifier
from sklearn.externals import joblib
from sklearn.cross_validation import KFold
ITERATION = 5
class dataPreprocessing(object):
def __init__(self):
#self.iterations = str(iterations)
pass
'''
func : loadEvalOrTrainData
Input : dataPath, labelPath with different format
Meaning: To support the different format of data like txt json or xml
Output : data / label with the format of numpy
'''
'''
把迭代次数的定义 放在这是为了以后 额外的模板 和 标准模板都有最原始的基类
'''
def loadXmlData(self):
'''
pre-load the xml foramt transform to numpy
'''
pass
def loadJsonData(self):
'''
pre-load the json foramt transform to numpy
'''
pass
def loadTxtData(self,dataPath,labelPath):
data = np.loadtxt(dataPath)
label = np.loadtxt(labelPath,dtype='int32')
return data, label
pass
def dataTestPort(self,dataPath):
# --- 忽略 labelPath 的警告 ------
with warnings.catch_warnings():
warnings.simplefilter("ignore")
data,label = self.loadTxtData(dataPath,[])
return data
pass
def dataEvalPort(self,dataPath,labelPath):
data, label = self.loadTxtData(dataPath,labelPath)
return data, label
pass
def dataTrainPort(self,dataPath,labelPath):
data, label = self.loadTxtData(dataPath,labelPath)
return data, label
pass
def dataStackPort(self,dataPath,labelPath):
data, label = self.loadTxtData(dataPath,labelPath)
return data, label
pass
'''
func: standard_classification_template
Meaning: Ease for use of sklearn-style model
Introduction:
train : train this model with data and label
test : test this model with data
eval : K - fold eval this model with data and label
'''
class standard_classification_template(dataPreprocessing):
def __init__(self):
#super(standard_classification_template,self).__init__()
#self.iterations = str(1)
self.stageName = 'stage -'
self.modelName = 'model'
self.methodName = 'svm'
self.random_state = 48
self.Kfold = 5
pass
def iterDefine(self):
self.iterations = str(100)
def showEvalResult(self, Top2Preds, labelTest):
totalNum = len(labelTest)
rightNum = 0
for preds, trueLabel in zip(Top2Preds, labelTest):
jugTag = self.jugResult(preds, trueLabel)
if jugTag == True:
rightNum = rightNum + 1
print('Precision = ', rightNum/totalNum)
return rightNum/totalNum
pass
def jugResult(self,preds, trueLabel):
for num in preds:
if num == trueLabel:
return True
return False
def TopSelect(self, sortRank, N):
TopSelect = sortRank[0:N]
return TopSelect
def TopPredict(self,preds):
TopPreds = []
for index in preds:
SortRank = np.argsort(-index)
TopN = self.TopSelect(SortRank, 2)
TopPreds.append(TopN)
return TopPreds
def dataNormalized(self,data):
data = np.transpose(data)
X_scaled = preprocessing.scale(data)
X_scaled = np.transpose(X_scaled)
return X_scaled
pass
def establishModel(self):
model = SVC(C = 0.01, kernel='linear',shrinking=True,decision_function_shape='ovo',random_state= 48,probability=True)
return model
pass
def testProb(self,data):
data = self.dataNormalized(data)
try:
model = joblib.load( self.modelName )
except FileNotFoundError:
print('Hey man, model not found , please train this model first!')
return
probModel = model.predict_proba(data)
return probModel
pass
def testProcess(self,data):
probModel = self.testProb(data)
TopPreds = self.TopPredict(probModel)
return TopPreds
pass
def trainProcess(self,data,label):
data = self.dataNormalized(data)
model = self.establishModel()
model.fit(data,label)
print(self.modelName)
joblib.dump(model, self.modelName )
def train(self,dataPath,labelPath):
'''
基于树的模型不用做归一化,基于统计的模型需要归一化
'''
data,label = dataPreprocessing.dataTrainPort(self, dataPath, labelPath)
self.trainProcess(data,label)
pass
def test(self,dataPath):
data = dataPreprocessing.dataTestPort(self, dataPath)
return self.testProcess(data)
def KFoldEval(self,dataPath,labelPath):
data, label = dataPreprocessing.dataEvalPort(self, dataPath, labelPath)
data = self.dataNormalized(data)
kf = KFold(len(label),n_folds = self.Kfold, random_state = self.random_state)
'''
func : describe the 5-fold cross-valdation
mean: for each fold we describe it
'''
foldCount = 1
score = []
for trainIndex, testIndex in kf:
print('this is the',foldCount,'th fold')
dataTrain, dataTest, labelTrain, labelTest = data[trainIndex], data[testIndex], label[trainIndex], label[testIndex]
model = self.establishModel()
model.fit(dataTrain,labelTrain)
probModel = model.predict_proba(dataTest)
TopPreds = self.TopPredict(probModel)
tmpScore = self.showEvalResult(TopPreds, labelTest)
score.append(tmpScore)
foldCount = foldCount + 1
pass
avgScore = sum(score)/len(score)
print('k - fold score is ',avgScore)
def stackingStage1FeatureAndLabel(self,data,label):
#print('data',len(data))
#print('label',len(label))
#self.iterDefine()
#data, label = dataPreprocessing.dataEvalPort(self, data, label)
'''
Test with K trained models and ensemble with the test data
example:
k - 1 fold train 1 test test result
' '
' concat test result '
' '
k - 1 fold train 1 test test result
'''
kf = KFold(len(label), n_folds = self.Kfold, random_state = self.random_state)
foldCount = 1
self.standardModelName = self.modelName
probSet = []
labelSet = []
for trainIndex, testIndex in kf:
#print('trainIndex',trainIndex)
#print('testIndex',testIndex)
print('this is the',foldCount,'th fold stacking to extract data and label')
dataTrain, dataTest, labelTrain, labelTest = data[trainIndex], data[testIndex], label[trainIndex], label[testIndex]
'''
change the name while k-fold training processing
'''
#self.tmpModelName = self.methodName + self.modelName + str(foldCount)
self.tmpModelName = self.stageName + self.iterations + self.methodName + self.modelName + str(foldCount)
self.modelName = self.tmpModelName
'''
train process
'''
probModel = self.testProb(dataTest)
probSet.extend(probModel)
labelSet.extend(labelTest)
'''
需要把 probSet 再反转过来 现在是 5 4 3 2 1 变成 1 2 3 4 5
'''
self.modelName = self.standardModelName
foldCount = foldCount + 1
return probSet,labelSet
def stackingTrainPort(self,data,label):
#self.iterDefine()
#try :
# data,label = dataPreprocessing.dataStackPort(self,dataPath,labelPath)
#except ValueError:
# data = dataPath , label = labelPath
'''
example:
k - 1 fold train 1 test trained model 1
' '
' '
' '
k - 1 fold train 1 test trained model N
'''
'''
stackTrain 和 stackTest 的数据集合不应是同一集合
'''
'''
实际调用的时候没有 数据的文件传输借口
'''
#data, label = dataPreprocessing.dataEvalPort(self, data, label)
'''
data and label is numpy format data , not txt file
'''
kf = KFold(len(label), n_folds = self.Kfold, random_state = self.random_state)
'''
stackPort 分数据集的时候 需要 分相同的 K 份
'''
foldCount = 1
self.standardModelName = self.modelName
for trainIndex, testIndex in kf:
#print('trainIndex',trainIndex)
#print('testIndex',testIndex)
print('this is the',foldCount,'th fold stacking')
dataTrain, dataTest, labelTrain, labelTest = data[trainIndex], data[testIndex], label[trainIndex], label[testIndex]
'''
change the name while k-fold training processing
'''
self.tmpModelName = self.stageName + self.iterations + self.methodName + self.modelName + str(foldCount)
self.modelName = self.tmpModelName
'''
train process
'''
self.trainProcess(dataTrain,labelTrain)
self.modelName = self.standardModelName
foldCount = foldCount + 1
def stackingTestPort(self,data):
#self.iterDefine()
'''
example:
train :
k - 1 fold train 1 test trained model 1
' '
' '
' '
k - 1 fold train 1 test trained model N
test:
trained model 1 result 1
' '
testData trained model m result m
' '
trained model n result n
'''
'''
stackTrain 和 stackTest 的数据集合不应是同一集合 它们的并集是完整集合 交集为0
'''
'''
实际调用的时候没有 数据的文件传输借口
'''
#data = dataPreprocessing.dataTestPort(self, data)
foldCount = 1
self.standardModelName = self.modelName
sumProb = 0
for i in range(self.Kfold):
#self.tmpModelName = self.methodName + self.modelName + str(foldCount)
self.tmpModelName = self.stageName + self.iterations + self.methodName + self.modelName + str(foldCount)
self.modelName = self.tmpModelName
#print('modelName',self.modelName)
probModel = self.testProb(data)
self.modelName = self.standardModelName
foldCount = foldCount + 1
try:
sumProb = sumProb + probModel
except TypeError:
print('Lost model name is ' + self.tmpModelName + '!')
return
avgProb = sumProb / self.Kfold
return avgProb
pass
'''
func : reconstruct the code with inherit
'''
class svmMethod(standard_classification_template):
def __init__(self):
super(svmMethod, self).__init__()
self.methodName = 'svm'
#self.iterations = ''
pass
def establishModel(self):
#model = SVC(C = 0.01, kernel='linear',shrinking=True,decision_function_shape='ovo',random_state= 48,probability=True)
model = SVC(C = 0.01, kernel='rbf',shrinking = True,decision_function_shape='ovo',random_state= 48,probability=True)
return model
pass
'''
为了增加可读性 需要再在子类添加一次接口的声明
'''
def train(self,dataPath,labelPath):
print('start svm train')
super().train(dataPath, labelPath)
def test(self,dataPath):
print('start svm test')
return super().test(dataPath)
def KFoldEval(self,dataPath,labelPath):
super().KFoldEval(dataPath,labelPath)
'''
stack 相关的三个接口输入是 numpy 格式的 数据 方便上级接口调用 而不是 file 格式的输入
'''
def stackingTrainPort(self,data,label):
super().stackingTrainPort(data,label)
def stackingTestPort(self,data):
return super().stackingTestPort(data)
def stackingStage1FeatureAndLabel(self,data,label):
probSet,labelSet = super().stackingStage1FeatureAndLabel(data,label)
return probSet,labelSet
class lrMethod(standard_classification_template):
def __init__(self):
super(lrMethod, self).__init__()
self.methodName = 'lr'
#self.iterations = ''
pass
def establishModel(self):
model = linear_model.LogisticRegression(penalty='l2', dual = False, C = 50, solver='newton-cg',max_iter=1000)
#model = linear_model.LogisticRegression( dual = False, C = 0.01, solver='newton-cg',max_iter=1000)
return model
pass
'''
为了增加可读性 需要再在子类添加一次接口
'''
def train(self,dataPath,labelPath):
print('start lr train')
super().train(dataPath, labelPath)
def test(self,dataPath):
print('start lr test')
return super().test(dataPath)
def KFoldEval(self,dataPath,labelPath):
super().KFoldEval(dataPath,labelPath)
'''
stack 相关的三个接口输入是 numpy 格式的 数据 方便上级接口调用 而不是 file 格式
'''
def stackingTrainPort(self,data,label):
super().stackingTrainPort(data,label)
def stackingTestPort(self,data):
return super().stackingTestPort(data)
def stackingStage1FeatureAndLabel(self,data,label):
probSet,labelSet = super().stackingStage1FeatureAndLabel(data,label)
return probSet,labelSet
class rfMethod(standard_classification_template):
def __init__(self):
super(rfMethod, self).__init__()
self.methodName = 'rf'
#self.iterations = ''
pass
'''
tree based model is meaningless to use daraNormalized, so we use origin data
'''
def dataNormalized(self,data):
return data
pass
def establishModel(self):
model = RandomForestClassifier(criterion='entropy',n_estimators=62 ,n_jobs=-1)
return model
pass
'''
为了增加可读性 需要再在子类添加一次接口
'''
def train(self,dataPath,labelPath):
print('start rf train')
super().train(dataPath, labelPath)
def test(self,dataPath):
print('start rf test')
return super().test(dataPath)
def KFoldEval(self,dataPath,labelPath):
super().KFoldEval(dataPath,labelPath)
'''
stack 相关的三个接口输入是 numpy 格式的 数据 方便上级接口调用 而不是 file 格式的输入
'''
def stackingTrainPort(self,data,label):
super().stackingTrainPort(data,label)
def stackingTestPort(self,data):
return super().stackingTestPort(data)
def stackingStage1FeatureAndLabel(self,data,label):
probSet,labelSet = super().stackingStage1FeatureAndLabel(data,label)
return probSet,labelSet
class nbMethod(standard_classification_template):
def __init__(self):
super(nbMethod, self).__init__()
self.methodName = 'nb'
#self.iterations = ''
pass
def establishModel(self):
model = GaussianNB()
#model = MultinomialNB(alpha = 1.3, fit_prior=True)
return model
pass
'''
为了增加可读性 需要再在子类添加一次接口
'''
def train(self,dataPath,labelPath):
print('start nb train')
super().train(dataPath, labelPath)
def test(self,dataPath):
print('start nb test')
return super().test(dataPath)
def KFoldEval(self,dataPath,labelPath):
super().KFoldEval(dataPath,labelPath)
'''
stack 相关的三个接口输入是 numpy 格式的 数据 方便上级接口调用 而不是 file 格式的输入
'''
def stackingTrainPort(self,data,label):
super().stackingTrainPort(data,label)
def stackingTestPort(self,data):
return super().stackingTestPort(data)
def stackingStage1FeatureAndLabel(self,data,label):
probSet,labelSet = super().stackingStage1FeatureAndLabel(data,label)
return probSet,labelSet
if __name__ == '__main__':
dataPath = 'D:/2017_8_8_task/20170808/SAR/task_classification/comp_dataset/fc7.txt'
labelPath = 'D:/2017_8_8_task/test/Stacking_test/labelinfo.txt'
model = lrMethod()
''' 单元测试 - 1 '''
model.train(dataPath,labelPath)
model.test(dataPath)
model.KFoldEval(dataPath,labelPath)
print('pass the test stage 1')
''' stack 接口测试 '''
#data = np.loadtxt(dataPath)
#label = np.loadtxt(labelPath)
#model.stackingTrainPort(data,label)
#result = model.stackingTestPort(data)
#probSet,labelSet = model.stackingStage1FeatureAndLabel(data,label)
#print('pass the test stage 2')