/
__classifiers.py
194 lines (151 loc) · 5.62 KB
/
__classifiers.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
# from experiments._1_one_user_learn_neighbours.fit_social_models import *
from __future__ import print_function
from tw_dataset.settings import DATASETS_FOLDER
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import GridSearchCV, StratifiedKFold
# from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import classification_report
from experiments.grid_params import GRID_PARAMS
from os.path import join
import numpy as np
from random import sample
def evaluate_model(clf, X_train, X_test, y_train, y_test):
y_true, y_pred = y_train, clf.predict(X_train)
print("Detailed classification report:\n")
print("Scores on training set.\n")
print(classification_report(y_true, y_pred))
y_true, y_pred = y_test, clf.predict(X_test)
print("Scores on test set.\n")
print(classification_report(y_true, y_pred))
def sub_sample_negs_arr(X, y):
npos = int(sum(y))
neg_inds = [i for i in range(len(y)) if y[i] == 0]
pos_inds = [i for i in range(len(y)) if y[i]]
sample_neg_inds = sample(neg_inds, npos)
inds = pos_inds + sample_neg_inds
Xs = X[inds,:]
ys = y[inds]
return Xs, ys
def model_select_rdf(dataset, cv=3, n_jobs=6):
X_train, X_test, y_train, y_test = dataset
w1 = sum(y_train)/len(y_train)
w0 = 1 - w1
sample_weight = np.array([w0 if x==0 else w1 for x in y_train])
# Set the parameters by cross-validation
params = GRID_PARAMS['rdf']
scores = [
# 'recall',
'f1',
# 'precision',
]
for score in scores:
print("# Tuning hyper-parameters for %s" % score)
print()
clf = GridSearchCV(
RandomForestClassifier(),
param_grid=params, # parameters to tune via cross validation
refit=True, # fit using all data, on the best detected classifier
n_jobs=n_jobs, # number of cores to use for parallelization; -1 for "all cores"
scoring=score, # what score are we optimizing?
cv=cv, # what type of cross validation to use
verbose=10,
)
clf.fit(X_train, y_train)
print("Best parameters set found on training set:")
print()
print(clf.best_params_)
print("Detailed classification report:")
print()
print("Scores on training set.")
y_true, y_pred = y_train, clf.predict(X_train)
print(classification_report(y_true, y_pred))
print()
print("Scores on test set.")
print()
y_true, y_pred = y_test, clf.predict(X_test)
print(classification_report(y_true, y_pred))
print()
return clf
def model_select_svc(dataset, cv=3, n_jobs=6, max_iter=None):
X_train, X_test, y_train, y_test = dataset
# Set the parameters by cross-validation
parameters = GRID_PARAMS['svc']
if max_iter:
parameters['max_iter'] = [max_iter]
scores = [
# 'precision',
# 'recall',
'f1'
]
for score in scores:
print("# Tuning hyper-parameters for %s" % score)
print()
clf = GridSearchCV(
SVC(),
param_grid=parameters, # parameters to tune via cross validation
refit=True, # fit using all data, on the best detected classifier
n_jobs=n_jobs, # number of cores to use for parallelization; -1 for "all cores"
scoring=score, # what score are we optimizing?
cv=cv, # what type of cross validation to use
# verbose=10
)
clf.fit(X_train, y_train)
print("Best parameters set found on training set:")
print()
print(clf.best_params_)
print("Detailed classification report:")
print()
print("Scores on training set.")
y_true, y_pred = y_train, clf.predict(X_train)
print(classification_report(y_true, y_pred))
print()
print("Scores on test set.")
print()
y_true, y_pred = y_test, clf.predict(X_test)
print(classification_report(y_true, y_pred))
print()
return clf
def model_select_sgd(dataset, cv=3, n_jobs=6):
X_train, X_test, y_train, y_test = dataset
# Set the parameters by cross-validation
parameters = {
'alpha': (0.01, 0.001, 0.00001),
'penalty': ('l1', 'l2', 'elasticnet'),
'loss': ('hinge', 'log'),
'n_iter': (10, 50, 80),
}
scores = [
# 'precision',
# 'recall',
'f1'
]
for score in scores:
print("# Tuning hyper-parameters for %s" % score)
print()
clf = GridSearchCV(
SGDClassifier(),
param_grid=parameters, # parameters to tune via cross validation
refit=True, # fit using all data, on the best detected classifier
n_jobs=n_jobs, # number of cores to use for parallelization; -1 for "all cores"
scoring=score, # what score are we optimizing?
cv=cv, # what type of cross validation to use
verbose=10,
)
clf.fit(X_train, y_train)
print("Best parameters set found on training set:")
print()
print(clf.best_params_)
print("Detailed classification report:")
print()
print("Scores on training set.")
y_true, y_pred = y_train, clf.predict(X_train)
print(classification_report(y_true, y_pred))
print()
print("Scores on test set.")
print()
y_true, y_pred = y_test, clf.predict(X_test)
print(classification_report(y_true, y_pred))
print()
return clf