-
Notifications
You must be signed in to change notification settings - Fork 0
/
MushroomClassifiers.py
192 lines (158 loc) · 6.72 KB
/
MushroomClassifiers.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
import pandas as pd
import numpy as np
from sklearn.model_selection import KFold, train_test_split, cross_validate
import matplotlib.pyplot as plt
# Models for classification task
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from models.EntityEmbeddingModel import EntityEmbedding
from util.utilities import label, replace
from sacred import Experiment
from sacred.observers import MongoObserver
ex = Experiment("Airline Delay Prediction-- Entity Embedding input")
ex.observers.append(MongoObserver())
@ex.config
def my_config():
oneHotEncoding_input = False
EntityEmbedding_input = True
size = 5000
@ex.capture
def getdata(size):
data = pd.read_csv("data/airline.csv")
data = data.drop(['Flight', 'Time'], axis=1)
data = data[:size]
return data
def getOneHotEncoding(X):
X = pd.get_dummies(X, columns=['Airline', 'AirportFrom', 'AirportTo', 'DayOfWeek'])
return X
def getEntityEmbedding(X, y, data):
# Apply Label Encoding to the Categorical features
label(data, 'Airline')
label(data, 'AirportFrom')
label(data, 'AirportTo')
# Apply entity Embedding to the Categorical variables
embeddingModel = EntityEmbedding()
embeddingModel.add('Airline', input_shape=18, output_shape=8)
embeddingModel.add('AirportFrom', input_shape=293, output_shape=10)
embeddingModel.add('AirportTo', input_shape=293, output_shape=10)
embeddingModel.add('DayOfWeek', input_shape=7, output_shape=5)
embeddingModel.dense('Length', output_shape=1)
embeddingModel.concatenate()
X = data.loc[:, ['Airline', 'AirportFrom', 'AirportTo', 'DayOfWeek', 'Length']]
X['Airline'] = X['Airline'].astype(float, errors='raise')
X['AirportFrom'] = X['AirportFrom'].astype(np.float32)
X['AirportTo'] = X['AirportTo'].astype(np.float32)
X['DayOfWeek'] = X['DayOfWeek'].astype(np.float32)
X_train, X_ee, y_train, y_ee = train_test_split(X, y, test_size=0.25, random_state=44)
embeddingModel.fit(X_ee, y_ee, X_train, y_train, epochs=12)
weights = embeddingModel.get_weight()
X = replace(X, weights, embeddingModel.embeddings)
return X
@ex.capture
def FeatureEncoder(X, y, data, oneHotEncoding_input, EntityEmbedding_input):
if oneHotEncoding_input:
X = getOneHotEncoding(X)
elif EntityEmbedding_input:
X = getEntityEmbedding(X, y, data)
return X
@ex.capture
def plotGraph(results_accuracy, results_recall, results_precision, results_f1, names,
oneHotEncoding_input, EntityEmbedding_input):
# boxplot algorithm comparison
fig = plt.figure()
if oneHotEncoding_input:
fig.suptitle('Algorithm Comparison-- OneHotEncoding (Recall)')
elif EntityEmbedding_input:
fig.suptitle('Algorithm Comparison-- Entity Embedding (Recall)')
ax = fig.add_subplot(111)
plt.boxplot(results_recall)
ax.set_xticklabels(names)
plt.ylabel("Recall")
plt.show()
if oneHotEncoding_input:
fig.savefig('plots/OneHotEncoding_Recall.png')
elif EntityEmbedding_input:
fig.savefig('plots/EntityEmbedding_Recall.png')
# boxplot algorithm comparison
fig = plt.figure()
if oneHotEncoding_input:
fig.suptitle('Algorithm Comparison-- OneHotEncoding (Accuracy)')
elif EntityEmbedding_input:
fig.suptitle('Algorithm Comparison-- Entity Embedding (Accuracy)')
ax = fig.add_subplot(111)
plt.boxplot(results_accuracy)
ax.set_xticklabels(names)
plt.ylabel("Accuracy")
plt.show()
if oneHotEncoding_input:
fig.savefig('plots/OneHotEncoding_Accuracy.png')
elif EntityEmbedding_input:
fig.savefig('plots/EntityEmbedding_Accuracy.png')
# boxplot algorithm comparison
fig = plt.figure()
if oneHotEncoding_input:
fig.suptitle('Algorithm Comparison-- OneHotEncoding (Precision)')
elif EntityEmbedding_input:
fig.suptitle('Algorithm Comparison-- Entity Embedding (Precision)')
ax = fig.add_subplot(111)
plt.boxplot(results_precision)
ax.set_xticklabels(names)
plt.ylabel("Precision")
plt.show()
if oneHotEncoding_input:
fig.savefig('plots/OneHotEncoding_Precision.png')
elif EntityEmbedding_input:
fig.savefig('plots/EntityEmbedding_Precision.png')
# boxplot algorithm comparison
fig = plt.figure()
if oneHotEncoding_input:
fig.suptitle('Algorithm Comparison-- OneHotEncoding (f1 score)')
elif EntityEmbedding_input:
fig.suptitle('Algorithm Comparison-- Entity Embedding (f1 score)')
ax = fig.add_subplot(111)
plt.boxplot(results_f1)
ax.set_xticklabels(names)
plt.ylabel("f1 score")
plt.show()
if oneHotEncoding_input:
fig.savefig('plots/OneHotEncoding_f1.png')
elif EntityEmbedding_input:
fig.savefig('plots/EntityEmbedding_f1.png')
@ex.automain
def my_main():
data = getdata()
X = data.loc[:, ['Airline', 'AirportFrom', 'AirportTo', 'DayOfWeek', 'Length']]
y = data.Delay
#Get encoded Feature vectors for categorical data
X= FeatureEncoder(X, y , data)
models = [('LR', LogisticRegression(max_iter=1000)), ('DTC', DecisionTreeClassifier()),
('KNN', KNeighborsClassifier()),
('NB', GaussianNB()),
# ('SVM', SVC(kernel="linear")),
('RFC', RandomForestClassifier(max_depth=5, n_estimators=10)),
('GBC', GradientBoostingClassifier())]
results_accuracy = []
results_recall = []
results_precision = []
results_f1 = []
names = []
seed = 7
scoring = ['accuracy', 'recall', 'f1', 'precision']
for name, model in models:
kfold = KFold(n_splits=10)
cv_results = cross_validate(model, X, y, cv=kfold, scoring=scoring)
results_recall.append(cv_results['test_recall'])
results_accuracy.append(cv_results['test_accuracy'])
results_precision.append(cv_results['test_precision'])
results_f1.append(cv_results['test_f1'])
names.append(name)
msg = "%s: Accuracy: %f Recall: %f Precision: %f f1: %f" % (name, cv_results['test_accuracy'].mean(),
cv_results['test_recall'].mean(),
cv_results['test_precision'].mean(),
cv_results['test_f1'].mean())
print(msg)
plotGraph(results_accuracy, results_recall, results_precision, results_f1, names)