-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
256 lines (226 loc) · 11.4 KB
/
main.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
from decimal import ROUND_HALF_DOWN
import data_preprocessing.preprocessing as prep
from data_preprocessing.data_splitting import data_splitting
from classification.algorithms.decision_tree import DecisionTree
from nltk.tokenize import word_tokenize
from classification.classification import Classifier
import classification.algorithms.my_decision_tree as dt
from classification.algorithms.naive_bayes import NaiveBayes
from classification.algorithms.svm import SVM
import matplotlib.pyplot as plt
import numpy as np
import time
def main():
#preProcessing the data
userInput = input('Give 3 files: ').split()
data = prep.preProcessing
data.merge(userInput)
K = 1500
rows, cols = (len(data.line),len(data.l))
words_list = data.l
#D =data.init_matrix(data.line, data.l)
# split the data into training, validation, testing
raw = data_splitting(data.line, data.y)
X_train, X_val, X_test, y_train, y_val, y_test = raw.data_splitting()
feature = data.init_matrix(X_train, words_list)
# feature selection
# choose top k words
top_k_words = data.top_k_words(words_list, K)
langs = ['DT', 'DT(implemented)', 'MultinomialNB', 'SVM']
rawFeatureTime = []
rawFeatureTrainAccuracy =[]
rawFeatureValidAccuracy =[]
rawFeatureTestAccuracy =[]
reducedFeatureTime = []
reducedFeatureTrainAccuracy =[]
reducedFeatureValidAccuracy =[]
reducedFeatureTestAccuracy =[]
print('---------------------------Decision Tree---------------------------')
#apply classification
start_time = time.time()
classifier = Classifier(DecisionTree(2, 11))
classifier.train(feature, y_train)
train_accuracy = classifier.getAccuracy(feature, y_train)
val_accuracy = classifier.getAccuracy(data.init_matrix(X_val, words_list), y_val)
test_accuracy = classifier.getAccuracy(data.init_matrix(X_test, words_list), y_test)
print(f'The Accuracy of Training data is {train_accuracy}')
print(f'The Accuracy of Validation data is {val_accuracy}')
print(f'The Accuracy of Testing data is {test_accuracy}')
print("sklearn decision tree time--- %s seconds ---" % (time.time() - start_time))
#add data
rawFeatureTime.append(time.time() - start_time)
rawFeatureTrainAccuracy.append(train_accuracy)
rawFeatureValidAccuracy.append(val_accuracy)
rawFeatureTestAccuracy.append(test_accuracy)
for i, row in enumerate(feature):
row.append(y_train[i])
start_time = time.time()
root =dt.build_tree(feature, 2, 11)
train_accuracy = dt.getAccuracy(root, data.init_matrix(X_train, words_list), y_train)
val_accuracy = dt.getAccuracy(root, data.init_matrix(X_val, words_list), y_val)
test_accuracy = dt.getAccuracy(root, data.init_matrix(X_test, words_list), y_test)
print(f'The Accuracy of Training data is {train_accuracy}')
print(f'The Accuracy of Validation data is {val_accuracy}')
print(f'The Accuracy of Testing data is {test_accuracy}')
print("My decision tree time--- %s seconds ---" % (time.time() - start_time))
rawFeatureTime.append(time.time() - start_time)
rawFeatureTrainAccuracy.append(train_accuracy)
rawFeatureValidAccuracy.append(val_accuracy)
rawFeatureTestAccuracy.append(test_accuracy)
print('---------------------------Naive Bayes---------------------------')
feature = data.init_matrix(X_train, words_list)
start_time = time.time()
classifier = Classifier(NaiveBayes())
classifier.train(feature, y_train)
train_accuracy = classifier.getAccuracy(feature, y_train)
val_accuracy = classifier.getAccuracy(data.init_matrix(X_val, words_list), y_val)
test_accuracy = classifier.getAccuracy(data.init_matrix(X_test, words_list), y_test)
print(f'The Accuracy of Training data is {train_accuracy}')
print(f'The Accuracy of Validation data is {val_accuracy}')
print(f'The Accuracy of Testing data is {test_accuracy}')
print("sklearn Naive Bayes time--- %s seconds ---" % (time.time() - start_time))
rawFeatureTime.append(time.time() - start_time)
rawFeatureTrainAccuracy.append(train_accuracy)
rawFeatureValidAccuracy.append(val_accuracy)
rawFeatureTestAccuracy.append(test_accuracy)
print('---------------------------SVM---------------------------')
start_time = time.time()
classifier = Classifier(SVM())
classifier.train(feature, y_train)
train_accuracy = classifier.getAccuracy(feature, y_train)
val_accuracy = classifier.getAccuracy(data.init_matrix(X_val, words_list), y_val)
test_accuracy = classifier.getAccuracy(data.init_matrix(X_test, words_list), y_test)
print(f'The Accuracy of Training data is {train_accuracy}')
print(f'The Accuracy of Validation data is {val_accuracy}')
print(f'The Accuracy of Testing data is {test_accuracy}')
print("sklearn SVM time--- %s seconds ---" % (time.time() - start_time))
rawFeatureTime.append(time.time() - start_time)
rawFeatureTrainAccuracy.append(train_accuracy)
rawFeatureValidAccuracy.append(val_accuracy)
rawFeatureTestAccuracy.append(test_accuracy)
# set width of bar
barWidth = 0.25
fig = plt.subplots(figsize =(12, 8))
# Set position of bar on X axis
br1 = np.arange(len(rawFeatureTrainAccuracy))
br2 = [x + barWidth for x in br1]
br3 = [x + barWidth for x in br2]
# Make the plot
plt.bar(br1, rawFeatureTrainAccuracy, color ='r', width = barWidth,
edgecolor ='grey', label ='orgFtAccuracy-Train')
plt.bar(br2, rawFeatureValidAccuracy, color ='g', width = barWidth,
edgecolor ='grey', label ='orgFtAccuracy-Valid')
plt.bar(br3, rawFeatureTestAccuracy, color ='b', width = barWidth,
edgecolor ='grey', label ='orgFtAccuracy-Test')
# Adding Xticks
plt.xlabel('Classification Algorithms', fontweight ='bold', fontsize = 15)
plt.ylabel('Accuracy', fontweight ='bold', fontsize = 15)
plt.xticks([r + barWidth for r in range(len(rawFeatureTrainAccuracy))],
langs)
plt.legend()
plt.show()
plt.close()
words_list = top_k_words
feature = data.init_matrix(X_train, words_list)
print('---------------------------Decision Tree (Reduced Feature)---------------------------')
#apply classification
start_time = time.time()
classifier = Classifier(DecisionTree(2, 11))
classifier.train(feature, y_train)
train_accuracy = classifier.getAccuracy(feature, y_train)
val_accuracy = classifier.getAccuracy(data.init_matrix(X_val, words_list), y_val)
test_accuracy = classifier.getAccuracy(data.init_matrix(X_test, words_list), y_test)
print(f'The Accuracy of Training data is {train_accuracy}')
print(f'The Accuracy of Validation data is {val_accuracy}')
print(f'The Accuracy of Testing data is {test_accuracy}')
print("sklearn decision tree time--- %s seconds ---" % (time.time() - start_time))
#add data
reducedFeatureTime.append(time.time() - start_time)
reducedFeatureTrainAccuracy.append(train_accuracy)
reducedFeatureValidAccuracy.append(val_accuracy)
reducedFeatureTestAccuracy.append(test_accuracy)
for i, row in enumerate(feature):
row.append(y_train[i])
start_time = time.time()
root =dt.build_tree(feature, 2, 11)
train_accuracy = dt.getAccuracy(root, data.init_matrix(X_train, words_list), y_train)
val_accuracy = dt.getAccuracy(root, data.init_matrix(X_val, words_list), y_val)
test_accuracy = dt.getAccuracy(root, data.init_matrix(X_test, words_list), y_test)
print(f'The Accuracy of Training data is {train_accuracy}')
print(f'The Accuracy of Validation data is {val_accuracy}')
print(f'The Accuracy of Testing data is {test_accuracy}')
print("My decision tree time--- %s seconds ---" % (time.time() - start_time))
reducedFeatureTime.append(time.time() - start_time)
reducedFeatureTrainAccuracy.append(train_accuracy)
reducedFeatureValidAccuracy.append(val_accuracy)
reducedFeatureTestAccuracy.append(test_accuracy)
print('---------------------------Naive Bayes (Reduced Feature)---------------------------')
feature = data.init_matrix(X_train, words_list)
start_time = time.time()
classifier = Classifier(NaiveBayes())
classifier.train(feature, y_train)
train_accuracy = classifier.getAccuracy(feature, y_train)
val_accuracy = classifier.getAccuracy(data.init_matrix(X_val, words_list), y_val)
test_accuracy = classifier.getAccuracy(data.init_matrix(X_test, words_list), y_test)
print(f'The Accuracy of Training data is {train_accuracy}')
print(f'The Accuracy of Validation data is {val_accuracy}')
print(f'The Accuracy of Testing data is {test_accuracy}')
print("sklearn Naive Bayes time--- %s seconds ---" % (time.time() - start_time))
reducedFeatureTime.append(time.time() - start_time)
reducedFeatureTrainAccuracy.append(train_accuracy)
reducedFeatureValidAccuracy.append(val_accuracy)
reducedFeatureTestAccuracy.append(test_accuracy)
print('---------------------------SVM (Reduced Feature)---------------------------')
start_time = time.time()
classifier = Classifier(SVM())
classifier.train(feature, y_train)
train_accuracy = classifier.getAccuracy(feature, y_train)
val_accuracy = classifier.getAccuracy(data.init_matrix(X_val, words_list), y_val)
test_accuracy = classifier.getAccuracy(data.init_matrix(X_test, words_list), y_test)
print(f'The Accuracy of Training data is {train_accuracy}')
print(f'The Accuracy of Validation data is {val_accuracy}')
print(f'The Accuracy of Testing data is {test_accuracy}')
print("sklearn SVM time--- %s seconds ---" % (time.time() - start_time))
reducedFeatureTime.append(time.time() - start_time)
reducedFeatureTrainAccuracy.append(train_accuracy)
reducedFeatureValidAccuracy.append(val_accuracy)
reducedFeatureTestAccuracy.append(test_accuracy)
# set width of bar
barWidth = 0.25
fig = plt.subplots(figsize =(12, 8))
# Set position of bar on X axis
br1 = np.arange(len(reducedFeatureTrainAccuracy))
br2 = [x + barWidth for x in br1]
br3 = [x + barWidth for x in br2]
# Make the plot
plt.bar(br1, reducedFeatureTrainAccuracy, color ='r', width = barWidth,
edgecolor ='grey', label ='reducedFtAccuracy-Train')
plt.bar(br2, reducedFeatureValidAccuracy, color ='g', width = barWidth,
edgecolor ='grey', label ='reducedFtAccuracy-Valid')
plt.bar(br3, reducedFeatureTestAccuracy, color ='b', width = barWidth,
edgecolor ='grey', label ='reducedFtAccuracy-Test')
# Adding Xticks
plt.xlabel('Classification Algorithms', fontweight ='bold', fontsize = 15)
plt.ylabel('Accuracy', fontweight ='bold', fontsize = 15)
plt.xticks([r + barWidth for r in range(len(rawFeatureTrainAccuracy))],
langs)
plt.legend()
plt.show()
plt.close()
br1 = np.arange(len(rawFeatureTime))
br2 = [x + barWidth for x in br1]
# Make the plot
plt.bar(br1, rawFeatureTime, color ='r', width = barWidth,
edgecolor ='grey', label ='rawFtTime')
plt.bar(br2, reducedFeatureTime, color ='g', width = barWidth,
edgecolor ='grey', label ='reducedFtTime')
# Adding Xticks
plt.xlabel('Classification Algorithms', fontweight ='bold', fontsize = 15)
plt.ylabel('Time (seconds)', fontweight ='bold', fontsize = 15)
plt.xticks([r + barWidth for r in range(len(rawFeatureTime))],
langs)
plt.legend()
plt.show()
plt.close()
if __name__ == '__main__':
main()