-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMLE_2_Classifier_Testing_And_Comparison.py
157 lines (108 loc) · 4.35 KB
/
MLE_2_Classifier_Testing_And_Comparison.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
#!python2
###############################################################################
# MachineLearningExperiment.py - Machine Learning Comic Book Cover Finding Experiment
# jamesj223
###############################################################################
# Initialisation
from datetime import datetime
import cPickle
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score, log_loss, precision_score, recall_score, f1_score, confusion_matrix
# ML Algorithms
from sklearn.dummy import DummyClassifier # Lol
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC, LinearSVC, NuSVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
#from tqdm import tqdm
def warn(*args, **kwargs): pass
import warnings
warnings.warn = warn
warnings.simplefilter(action='ignore', category=FutureWarning)
###############################################################################
# Config
###############################################################################
# Classes
###############################################################################
# Functions
def save_obj(obj, name, protocol=2):
with open('obj/'+ name + '.pkl', 'wb') as f:
cPickle.dump(obj, f, protocol)
###############################################################################
# Main
if __name__ == '__main__':
startTime = datetime.now()
print("Start - " + str(datetime.now()))
print("")
### Code Goes Here
# Pandas/Train Stuff
train = pd.read_csv('TrainingSet2.csv')
y=train['label'].values
x = train.drop('label', 1)
# :( Need to figure out how to do this better
# File names are kinda important for what I want...
x = x.drop('fileName', 1)
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=0, test_size=0.2)
# SK Learn Pipe Stuff
# pipe = make_pipeline(
# StandardScaler(),
# #RandomForestClassifier(random_state=0)
# DummyClassifier(strategy="uniform")
# )
# pipe.fit(x_train, y_train)
# accuracyScore = accuracy_score(pipe.predict(x_test), y_test)
#print("Accuracy: {:.4%}".format(accuracyScore))
# Multi Classifier Showdown
classifiers = [
KNeighborsClassifier(3),
#SVC(kernel="linear", C=0.1, probability=True),
#NuSVC(probability=True),
DecisionTreeClassifier(),
RandomForestClassifier(),
AdaBoostClassifier(),
GradientBoostingClassifier(),
GaussianNB(),
LinearDiscriminantAnalysis(),
#QuadraticDiscriminantAnalysis()
]
for clf in classifiers:
name = clf.__class__.__name__
#pipe = make_pipeline(
# StandardScaler(),
# clf
#)
#pipe.fit(x_train, y_train)
clf.fit(x_train, y_train)
save_obj(clf, name)
train_predictions = clf.predict(x_test)
train_predictions_proba = clf.predict_proba(x_test)
print("="*30)
print(name)
#print('****Results****')
acc = accuracy_score(y_test, train_predictions)
print("Accuracy: {:.4%}".format(acc))
precision = precision_score(y_test, train_predictions)
print("Precision: {}".format(precision))
recall = recall_score(y_test, train_predictions)
print("Recall: {}".format(recall))
f1 = f1_score(y_test, train_predictions)
print("F1 Score: {}".format(f1))
logloss = log_loss(y_test, train_predictions_proba)
print("Log Loss: {}".format(logloss))
print("")
C = confusion_matrix(y_test, train_predictions)
print("True Positives: {}".format(C[1,1]))
print("True Neagtives: {}".format(C[0,0]))
print("False Positives: {}".format(C[0,1]))
print("False Negatives: {}".format(C[1,0]))
print("")
### Code Ends Here
print("End - " + str(datetime.now()))
print("Took: " + str( datetime.now() - startTime ))