-
Notifications
You must be signed in to change notification settings - Fork 0
/
classificationFunctions.py
281 lines (196 loc) · 10.3 KB
/
classificationFunctions.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
import numpy as np
from sklearn.linear_model import SGDClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB, MultinomialNB, BernoulliNB
from sklearn.metrics import roc_auc_score
from sklearn.metrics import classification_report, accuracy_score
from sklearn.model_selection import GridSearchCV, cross_val_score, train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import Imputer, StandardScaler, MaxAbsScaler
"""
Classification Models
Includes:
1. Gaussian NB
2. Multinominal NB
3. Kmeans
4. RandomForest
5. LinearSVM
6. NonLinearSVM
Methods:
performGaussianNB(X, y, folds=10, impStrategy= 'mean', preprocess=MaxAbsScaler(), priors=None)
-performs a Guassian NB
-Data should have a Normal Distribution
-can preSet Class priors
performMultiNomNB(X, y, binaryData=False, folds=5, impStrategy= 'mean', preprocess=MaxAbsScaler(),
aLow=0, aHigh=1, numAlphas=5, fit_prior=False, class_prior=None)
-performs a MultiNominal NB
-if Binary Data set binaryData to False
-Alpha is smoothness paramater tested in crossfold search
- 0 means no smoothness applied
performKNN(X, y, impStrategy= 'mean', preprocess=StandardScaler(), folds=5, nLow=1, nHigh=5, nIter=1):
-Knn which tests different amount of neighbor criterion per classifier
performRandomForest(X, y, folds=5, impStrategy= 'mean', class_weight=None, preprocess=StandardScaler(),
treeNumLow=10, treeNumhigh=11, treeNumLowIter=1)
- Tests different amount of trees per classifier
performLinearSVM(X, y, impStrategy= 'mean', preprocess=StandardScaler(), folds=5, penalty='l2', loss='hinge,
aLow = 0.0001, aHigh=.1, numAlphas=10, class_weight=None, l1RatLow=0, l1RatHigh=.5, numL1Ratios=10)
-test different hyparamters L1Ration, and Alphas through Hold out validation, with grid search
-penlatly 'l2', 'l1', or 'elasticnet'
-l2 and net lead to sparse data
-Use when LOOK INTO MORE
loss
- 'hinge'- deafult
- 'log'- logisitic regression
- ‘modified_huber'- good if outliers
- ‘squared_hinge’- quadratcillay penalized
class_weight
-set if class imbalance
L1Ration
- 0 to 1
-percent of L1 versus l2 in model
-high l1 means nonImportant features in Data
Alpha
-regualarization term
performNoNLinearSVM(X, y, kernel='rbf', impStrategy= 'mean', preprocess=StandardScaler(), folds=5, cLow= 1, cHigh=10,
numCs=10, gammaLow=0, gammaHigh=1, numGammas=10, class_weight=None)
-test different hyparamters c, and gamma through hold out validation, with grid search
-kernel
-'rbf', 'sigmoid', 'poly'
-C
-soft margin param
-Larger C == bigger soft Margin
- Use K cross validation to find
-gamma
-larger gamma leads to high bias low variance
-class_weight
-set if class imbalance
"""
def performGaussianNB(X, y, folds=10, impStrategy= 'mean', preprocess=MaxAbsScaler(), priors=None):
# create pipeline for Model testing/training
steps = [('imputation', Imputer(missing_values='NaN', strategy=impStrategy, axis=0)),
('scaler', preprocess),
('knn', GaussianNB(priors=priors))]
pipeline = Pipeline(steps)
# Get accuracy scores via Cross Validation
bestScore = np.mean(cross_val_score(pipeline, X, y, cv=folds))
# Compute and print metrics
print("Guassian NB Accuracy: {}\n".format(bestScore))
def performMultiNomNB(X, y, binaryData=False, folds=5, impStrategy= 'mean', preprocess=MaxAbsScaler(),
aLow=0, aHigh=1, numAlphas=5, fit_prior=False, class_prior=None):
# use hold out validation for analysis
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
#use bernulli or Multinominal Classifier based if binary data or not
if binaryData:
Nbclf= BernoulliNB(fit_prior=fit_prior, class_prior=class_prior)
else:
Nbclf= MultinomialNB(fit_prior=fit_prior, class_prior=class_prior)
# create pipeline for Model testing/training
#penalty, loss, and class weights
steps = [('imputation', Imputer(missing_values='NaN', strategy=impStrategy, axis=0)),
('scaler', preprocess),
('nb', Nbclf)]
pipeline = Pipeline(steps)
print(pipeline.get_params().keys())
# create different alphas to test
stepsize = (aHigh - aLow) / numAlphas
alphas = np.arange(aLow, aHigh, stepsize)
param_grid = {'nb__alpha': alphas}
# Create the GridSearchCV
gm_cv = GridSearchCV(pipeline, param_grid, cv=folds)
# fit the Grid Search Cross Value Model
gm_cv.fit(X_train, y_train)
# Compute and print metrics
print("NB Accuracy: {}\n".format(gm_cv.score(X_test, y_test)))
print("Best Alpha (smoothing paramter): " + str(gm_cv.best_params_.get('nb__alpha')))
def performKNN(X, y, impStrategy= 'mean', preprocess=StandardScaler(), folds=5, nLow=1, nHigh=5, nIter=1):
# use hold out validation for analysis
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=2)
# create pipeline for Model testing/training
steps = [('imputation', Imputer(missing_values='NaN', strategy=impStrategy, axis=0)),
('scaler', preprocess),
('knn', KNeighborsClassifier(random_state=2))]
pipeline = Pipeline(steps)
# compute KNN array
kSizes = np.arange(nLow, nHigh, nIter)
param_grid = {'knn__n_neighbors': kSizes}
# Create the GridSearchCV
gm_cv = GridSearchCV(pipeline, param_grid, cv=folds)
# fit the Grid Search Cross Value Model
gm_cv.fit(X_train, y_train)
# Compute and print metrics
print("Knn Accuracy: {}\n".format(gm_cv.score(X_test, y_test)))
print("Best Neighbor: " + str(gm_cv.best_params_.get('knn__n_neighbors')))
def performRandomForest(X, y, folds=5, impStrategy= 'mean', class_weight=None, preprocess=StandardScaler(),
treeNumLow=10, treeNumhigh=11, treeNumLowIter=1):
# use hold out validation for analysis
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=2)
# create pipeline for Model testing/training
steps = [('imputation', Imputer(missing_values='NaN', strategy=impStrategy, axis=0)),
('scaler', preprocess),
('forest', RandomForestClassifier(class_weight=class_weight))]
pipeline = Pipeline(steps)
# compute KNN array
treesNum = np.arange(treeNumLow, treeNumhigh, treeNumLowIter)
param_grid = {'forest__n_estimators': treesNum}
# Create the GridSearchCV
gm_cv = GridSearchCV(pipeline, param_grid, cv=folds)
# fit the Grid Search Cross Value Model
gm_cv.fit(X_train, y_train)
# Compute and print metrics
print("Random Forest Accuracy: {}\n".format(gm_cv.score(X_test, y_test)))
print("Best tree Amount: " + str(gm_cv.best_params_.get('forest__n_estimators')))
def performLinearSVM(X, y, impStrategy= 'mean', preprocess=StandardScaler(), folds=5, penalty='l2', loss='hinge', aLow = 0.0001, aHigh=.1, numAlphas=10,
class_weight=None, l1RatLow=0, l1RatHigh=.5, numL1Ratios=10):
# use hold out validation for analysis
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=2)
# create pipeline for Model testing/training
#penalty, loss, and class weights
steps = [('imputation', Imputer(missing_values='NaN', strategy=impStrategy, axis=0)),
('scaler', preprocess),
('linearSVM', SGDClassifier(penalty= penalty, loss=loss, class_weight=class_weight, random_state=2))]
pipeline = Pipeline(steps)
# create different alphas to test
stepsize = (aHigh - aLow) / numAlphas
alphas = np.arange(aLow, aHigh, stepsize)
# create different l1Ratios to test
stepsize = 1 / numL1Ratios
l1_ratios = np.arange(l1RatLow, l1RatHigh, stepsize)
param_grid = {'linearSVM__alpha': alphas,
'linearSVM__l1_ratio': l1_ratios}
# Create the GridSearchCV
gm_cv = GridSearchCV(pipeline, param_grid, cv=folds)
# fit the Grid Search Cross Value Model
gm_cv.fit(X_train, y_train)
# Compute and print metrics
print("Linear SVM Accuracy: {}\n".format(gm_cv.score(X_test, y_test)))
print("Best Alpha (Learning Rate): " + str(gm_cv.best_params_.get('linearSVM__alpha')))
print("Best L1 Ratio : " + str(gm_cv.best_params_.get('linearSVM__l1_ratio')))
def performNoNLinearSVM(X, y, kernel='rbf', impStrategy= 'mean', preprocess=StandardScaler(), folds=5, cLow=1, cHigh=10,
numCs=10,gammaLow=0, gammaHigh=1, numGammas=10, class_weight=None):
# use hold out validation for analysis
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=2)
# create pipeline for Model testing/training
#penalty, loss, and class weights
steps = [('imputation', Imputer(missing_values='NaN', strategy=impStrategy, axis=0)),
('scaler', preprocess),
('nonLinearSVM', SVC(kernel=kernel, probability=True, class_weight=class_weight, random_state=2))]
pipeline = Pipeline(steps)
# create different c (penalty) values to test
stepsize = (cHigh - cLow) / numCs
Cs = np.arange(cLow, cHigh, stepsize)
# create different l1Ratios to test
stepsize = 1 / numGammas
Gammas = np.arange(gammaLow, gammaHigh, stepsize)
param_grid = {'nonLinearSVM__C': Cs,
'nonLinearSVM__gamma': Gammas}
# Create the GridSearchCV
gm_cv = GridSearchCV(pipeline, param_grid, cv=folds)
# fit the Grid Search Cross Value Model
gm_cv.fit(X_train, y_train)
# Compute and print metrics
print("NonLinear SVM Accuracy: {}\n".format(gm_cv.score(X_test, y_test)))
print("Best C (Penalty Of Error term): " + str(gm_cv.best_params_.get('nonLinearSVM__C')))
print("Best Gamma Value : " + str(gm_cv.best_params_.get('nonLinearSVM__gamma')))
performNoNLinearSVM()