-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
executable file
·187 lines (145 loc) · 5.95 KB
/
models.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
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
"""Models configuration."""
from visualization_utils import visualize
# from metrics import precision, recall
import numpy as np
import pickle
np.random.seed(0)
class Model:
def __init__(self):
self.clf = None
self.dump = None
self.name = 'model'
def __str__(self):
return str(pickle.loads(self.dump))
def __repr__(self):
return str(pickle.loads(self.dump))
def fit(self, X_train, y_train):
""""""
self.clf = pickle.loads(self.dump)
self.clf.fit(X_train, y_train)
def predict(self, X_test):
""""""
return self.clf.predict(X_test)
def predict_proba(self, X_test):
""""""
return self.clf.predict_proba(X_test)
def execute(self, X_train, y_train, X_test, y_test, class_names):
"""
Performs model training and visualizes testing results.
Keyword arguments:
clf -- classifier object
name -- classifier title
X_train, y_train -- training set
X_test, y_test -- test set
"""
print(self.name)
# scores = cross_val_score(clf, X, labels, cv=20, scoring='accuracy')
# print Message().accuracy % (scores.mean(), scores.std() * 2)
# scores = cross_val_score(clf, X, labels, cv=20, scoring='precision')
# print Message().precision % (scores.mean(), scores.std() * 2)
# scores = cross_val_score(clf, X, labels, cv=20, scoring='recall')
# print Message().recall % (scores.mean(), scores.std() * 2)
self.fit(X_train, y_train)
predicted = self.predict(X_test)
visualize(y_test, predicted, class_names)
def save(self):
from sklearn.externals import joblib
joblib.dump(self.clf, self.save_path)
class SVM(Model):
def __init__(self, kernel='poly'):
from sklearn import svm
self.dump = pickle.dumps(
svm.SVC(kernel=kernel, decision_function_shape='ovr',
max_iter=2400, probability=True))
self.name = 'svm'
self.save_path = "models/%s.pkl" % (self.name, )
class LinearSVM(Model):
def __init__(self):
from sklearn import svm
self.dump = pickle.dumps(
svm.LinearSVC(max_iter=2400, probability=True))
self.name = "lsvm"
self.save_path = "models/%s.pkl" % (self.name, )
class KNN(Model):
def __init__(self, n_neighbors):
from sklearn.neighbors import KNeighborsClassifier
"""KNN with distance-based weight points"""
self.dump = pickle.dumps(
KNeighborsClassifier(n_neighbors, weights='distance'))
self.name = "knn"
self.save_path = "models/%s.pkl" % (self.name, )
class LinearRegression(Model):
def __init__(self):
from sklearn.linear_model import LinearRegression
self.dump = pickle.dumps(LinearRegression())
self.name = "linreg"
self.save_path = "models/%s.pkl" % (self.name, )
class LogRegression(Model):
def __init__(self):
from sklearn.linear_model import LogisticRegression
self.dump = pickle.dumps(LogisticRegression(solver='lbfgs',
multi_class='auto',
max_iter=800))
self.name = "logreg"
self.save_path = "models/%s.pkl" % (self.name, )
class GausNB(Model):
def __init__(self):
from sklearn.naive_bayes import GaussianNB
self.dump = pickle.dumps(GaussianNB())
self.name = "gpc"
self.save_path = "models/%s.pkl" % (self.name, )
class DecisionTree(Model):
def __init__(self):
from sklearn.tree import DecisionTreeClassifier
self.dump = pickle.dumps(
DecisionTreeClassifier(max_depth=None, min_samples_split=2,
random_state=0))
self.name = "dtree"
self.save_path = "models/%s.pkl" % (self.name, )
class RandomForest(Model):
def __init__(self, size=40):
from sklearn.ensemble import RandomForestClassifier
self.dump = pickle.dumps(RandomForestClassifier(n_estimators=size))
self.name = "rfc"
self.save_path = "models/%s_%d.pkl" % (self.name, size)
class ExtremelyRandomizedTrees(Model):
def __init__(self, size):
from sklearn.ensemble import ExtraTreesClassifier
self.dump = pickle.dumps(
ExtraTreesClassifier(n_estimators=size, max_depth=None,
min_samples_split=2, random_state=0))
self.name = "ertc"
self.save_path = "models/%s_%d.pkl" % (self.name, size)
class BaggingRandomForest(Model):
def __init__(self, size):
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import BaggingClassifier
self.dump = pickle.dumps(
BaggingClassifier(RandomForestClassifier(n_estimators=size),
max_samples=0.5, max_features=0.5))
self.name = "brfc"
self.save_path = "models/%s_%d.pkl" % (self.name, size)
class MLPC(Model):
def __init__(self, input_size):
from sklearn.neural_network import MLPClassifier
self.input_size = input_size
self.dump = pickle.dumps(MLPClassifier(solver='adam', alpha=1e-4,
learning_rate_init=1e-5,
hidden_layer_sizes=input_size,
random_state=1,
max_iter=4000))
self.name = "mlpc"
self.save_path = ("models/%s_%s.pkl" %
(self.name, "_".join([str(i) for i in input_size])))
class XGB(Model):
def __init__(self):
from xgboost import XGBClassifier
self.dump = pickle.dumps(XGBClassifier(objctive='multi:softmax'))
self.name = "xgb"
self.save_path = "models/%s.pkl" % (self.name, )
def main():
pass
if __name__ == '__main__':
main()