-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
151 lines (131 loc) · 5.66 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
from utils import trainClassifier, saveModel, getBestModel
import os
from sklearn.linear_model import LogisticRegression
from sklearn.svm import LinearSVC
from sklearn.naive_bayes import MultinomialNB
from sklearn.neighbors import KNeighborsClassifier
def getLinearSVM(X_train, y_train, X_val, y_val, path, progress=False, overwrite=True):
#C = [0.0001, 0.001, 0.01, 0.1, 1, 10, 100]
C = [0.1, 1, 10]
if os.path.exists(path)==False:
os.mkdir(path)
best_model = None
best_f1 = 0
ngram_lb = 1
ngram_ub = 2
if progress==True:
print("\n----- LINEAR SVM -----\n")
print("C\tAccuracy\tF1-Score\n===================================")
for c in C :
clf = LinearSVC(C=c, multi_class='ovr', class_weight='balanced', max_iter=100000)
for ngram_ub in range(1,5):
temp = trainClassifier(clf, ngram_lb, ngram_ub, X_train, y_train, X_val, y_val)
print(f"{c}\t{temp['acc']:.3f}\t\t{temp['f1_cv']:.3f}")
if temp['f1_cv'] <= best_f1:
continue
best_f1 = temp['f1_cv']
best_model = temp
else:
for c in C :
clf = LinearSVC(C=c, multi_class='ovr', class_weight='balanced', max_iter=100000)
for ngram_ub in range(1,5):
temp = trainClassifier(clf, ngram_lb, ngram_ub, X_train, y_train, X_val, y_val)
if temp['f1_cv'] <= best_f1:
continue
best_f1 = temp['f1_cv']
best_model = temp
saveModel('linsvm', best_model, path)
return getBestModel(path, 'linsvm', overwrite)
def getLogisticRegressor(X_train, y_train, X_val, y_val, path, progress=False, overwrite=True):
#C = [0.0001, 0.001, 0.001, 0.01, 0.1, 1, 10, 100]
C = [0.1, 1, 10]
if os.path.exists(path)==False:
os.mkdir(path)
best_model = None
best_f1 = 0
ngram_lb = 1
ngram_ub = 2
if progress==True:
print("\n----- LOGISTIC REGRESSION -----\n")
print("C\tAccuracy\tF1-Score\n===================================")
for c in C :
clf = LogisticRegression(C=c, multi_class='auto', class_weight='balanced', max_iter=100000)
for ngram_ub in range(1,5):
temp = trainClassifier(clf, ngram_lb, ngram_ub, X_train, y_train, X_val, y_val)
print(f"{c}\t{temp['acc']:.3f}\t\t{temp['f1_cv']:.3f}")
if temp['f1_cv'] <= best_f1:
continue
best_f1 = temp['f1_cv']
best_model = temp
else:
for c in C :
clf = LogisticRegression(C=c, multi_class='auto', class_weight='balanced', max_iter=100000)
for ngram_ub in range(1,5):
temp = trainClassifier(clf, ngram_lb, ngram_ub, X_train, y_train, X_val, y_val)
if temp['f1_cv'] <= best_f1:
continue
best_f1 = temp['f1_cv']
best_model = temp
saveModel('logreg', best_model, path)
return getBestModel(path, 'logreg', overwrite)
def getMNBClassifier(X_train, y_train, X_val, y_val, path, progress=False, overwrite=True):
#C = [0.0001, 0.001, 0.01, 0.1, 1, 10, 100]
C = [0.1, 1, 10]
if os.path.exists(path)==False:
os.mkdir(path)
best_model = None
best_f1 = 0
ngram_lb = 1
ngram_ub = 4
if progress==True:
print("\n----- MULTINOMIAL NAIVE BAYES -----\n")
print("C\tAccuracy\tF1-Score\n===================================")
for c in C :
clf = MultinomialNB(alpha=c)
for ngram_ub in range(1,5):
temp = trainClassifier(clf, ngram_lb, ngram_ub, X_train, y_train, X_val, y_val)
print(f"{c}\t{temp['acc']:.3f}\t\t{temp['f1_cv']:.3f}")
if temp['f1_cv'] <= best_f1:
continue
best_f1 = temp['f1_cv']
best_model = temp
else:
for c in C :
clf = MultinomialNB(alpha=c)
for ngram_ub in range(1,5) :
temp = trainClassifier(clf, ngram_lb, ngram_ub, X_train, y_train, X_val, y_val)
if temp['f1_cv'] <= best_f1:
continue
best_f1 = temp['f1_cv']
best_model = temp
saveModel('multinb', best_model, path)
return getBestModel(path, 'multinb', overwrite)
def getKNNClassifier(X_train, y_train, X_val, y_val, path, progress=False, overwrite=True):
n_neighbors = [i for i in range(3, 10, 2)]
if os.path.exists(path)==False:
os.mkdir(path)
best_model = None
best_f1 = 0
ngram_lb = 1
ngram_ub = 2
if progress==True:
print("\n----- KNN -----\n")
print("NN\tAccuracy\tF1-Score\n===================================")
for nn in n_neighbors :
clf = KNeighborsClassifier(n_neighbors=nn)
temp = trainClassifier(clf, ngram_lb, ngram_ub, X_train, y_train, X_val, y_val)
print(f"{nn}\t{temp['acc']:.3f}\t\t{temp['f1_cv']:.3f}")
if temp['f1_cv'] <= best_f1:
continue
best_f1 = temp['f1_cv']
best_model = temp
else:
for nn in n_neighbors :
clf = KNeighborsClassifier(n_neighbors=nn)
temp = trainClassifier(clf, ngram_lb, ngram_ub, X_train, y_train, X_val, y_val)
if temp['f1_cv'] <= best_f1:
continue
best_f1 = temp['f1_cv']
best_model = temp
saveModel('knn', best_model, path)
return getBestModel(path, 'knn', overwrite)