-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask_1.py
More file actions
163 lines (136 loc) · 4.74 KB
/
Task_1.py
File metadata and controls
163 lines (136 loc) · 4.74 KB
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
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import classification_report
from matplotlib.image import imread
import numpy as np
import glob
import pickle
import sys
names = ["Nearest Neighbor", "SVC Linear SVM", "SVC Gaussian Process",
"Decision Tree", "Random Forest", "MLP Classifier", "AdaBoost",
"Naive Bayes (GaussianNB)"]
classifiers = [
KNeighborsClassifier(3),
SVC(kernel="linear", C=0.025),
SVC(gamma=2, C=1),
DecisionTreeClassifier(max_depth=5),
RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
MLPClassifier(alpha=1),
AdaBoostClassifier(),
GaussianNB()]
def task1():
print('\nTASK 1')
print('------')
tr_dir = 'digits-train-5000'
v_dir = 'digits-validation-1000'
f_ext = '/*.png'
t_images, t_labels = load_dataset(tr_dir, f_ext)
v_images, v_labels = load_dataset(v_dir, f_ext)
t_images = reshape(t_images)
v_images = reshape(v_images)
flag = True
while flag:
print("--------------")
a = input("Begin Training? y/n : ")
if a == 'y' or a == 'Y':
train(t_images, t_labels)
flag = False
elif a == 'n' or a == 'N':
flag = False
else:
print("Invalid input \n")
flag = True
while flag:
print("--------------")
a = input("Begin Prediction? y/n : ")
if a == 'y' or a == 'Y':
predict(v_images, v_labels)
flag = False
elif a == 'n' or a == 'N':
flag = False
else:
print("Invalid input \n")
def load_dataset(set_n, ext):
data_set = set_n + ext
labels = []
images = []
print("loading dataset " + set_n + "...")
for file in glob.glob(data_set):
images.append(imread(file))
label = len(set_n)
label += 1
labels.append(file[label])
return images, labels
def reshape(arr):
arr = np.array(arr)
# print(arr.shape)
n_samples, nx, ny = arr.shape
d2_arr = arr.reshape((n_samples, nx * ny))
# print(d2_arr.shape)
return d2_arr
def train(t_images, t_labels):
print("\n")
for i in range(0, len(classifiers)):
print("Training ", str(names[i]))
current_classifier = classifiers[i]
current_classifier.fit(t_images, t_labels)
print("Saving model...\n")
try:
ext = ".pkl"
pkl_filename = str(names[i]+ext)
with open(pkl_filename, 'wb') as file:
pickle.dump(current_classifier, file)
except:
print("Error", sys.exc_info()[0], "occurred.")
i += 1
def predict(v_images, v_labels):
ext = ".pkl"
flag = True
while flag:
print("\nPrediction Main Menu")
print("1) Nearest Neighbor")
print("2) Linear SVM")
print("3) Gaussian Process")
print("4) Decision Tree")
print("5) Random Forest")
print("6) MLP")
print("7) AdaBoost")
print("8) Naive Bayes")
print("-")
print("9) All of the above")
print("-")
print("0) Return to Main Menu")
a = input("Which Classifier would you like to use? : ")
if a.isdigit():
a = int(a)
if a in range(1, 8):
print("prediction using: ", names[a-1])
# Load from file
filename = names[a-1] + ext
with open(filename, 'rb') as file:
pickle_model = pickle.load(file)
# Calculate the accuracy score and predict target values
score = pickle_model.score(v_images, v_labels)
print("Test score: ", (score*100), "%")
p_labels = pickle_model.predict(v_images)
print(classification_report(v_labels, p_labels))
elif a == 9:
for i in range(0, len(names)):
print("\nprediction using: ", str((i+1))+")", names[i])
# Load from file
filename = names[i] + ext
with open(filename, 'rb') as file:
pickle_model = pickle.load(file)
score = pickle_model.score(v_images, v_labels)
print("Test score: ", (score * 100), "%")
p_labels = pickle_model.predict(v_images)
print(classification_report(v_labels, p_labels))
elif a == 0:
flag = False
else:
print("invalid input")