-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSVM.py
180 lines (113 loc) · 3.89 KB
/
SVM.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
#!/usr/bin/env python
# coding: utf-8
# # Importing all the necessary modules
# In[1]:
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
import pickle
import gzip
from sklearn import svm
from sklearn.metrics import confusion_matrix
from PIL import Image
import os
from keras.utils import np_utils
# # Load MNIST dataset
# In[2]:
filename = 'mnist.pkl.gz'
f = gzip.open(filename, 'rb')
training_data, validation_data, test_data = pickle.load(f, encoding='latin1')
f.close()
# # Confusion Matrix Customization
# In[3]:
def plot_confusion_matrix(cm,
title='Confusion matrix',
cmap=None,
normalize=True):
import matplotlib.pyplot as plt
import numpy as np
import itertools
accuracy = np.trace(cm) / float(np.sum(cm))
misclass = 1 - accuracy
if cmap is None:
cmap = plt.get_cmap('Blues')
plt.figure(figsize=(8, 6))
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
thresh = cm.max() / 1.5 if normalize else cm.max() / 2
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
if normalize:
plt.text(j, i, "{:0.4f}".format(cm[i, j]),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
else:
plt.text(j, i, "{:,}".format(cm[i, j]),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label\naccuracy={:0.4f}; misclass={:0.4f}'.format(accuracy, misclass))
plt.show()
# # SVC
# In[ ]:
classifier = svm.SVC(kernel='linear')
model_fit = classifier.fit(training_data[0], training_data[1])
# # Validating and Testing on MNIST datasets
# In[ ]:
print('Validation Accuracy')
predictVal=model_fit.predict(validation_data[0])
print(classifier.score(validation_data[0], validation_data[1]))
print('Testing Accuracy')
SVM_prediction_mnist=model_fit.predict(test_data[0])
print(classifier.score(test_data[0], test_data[1]))
# # Confusion Matrix on MNIST testing dataset
# In[ ]:
conf_mat = confusion_matrix(test_data[1], SVM_prediction_mnist)
plot_confusion_matrix(cm = conf_mat,
normalize = False,
title = "Confusion Matrix")
# # Load USPS dataset
# In[ ]:
from keras.utils import np_utils
image_size = 28
num_labels = 10
USPSMat = []
USPSTar = []
curPath = 'USPSdata/Numerals'
savedImg = []
for j in range(0,10):
curFolderPath = curPath + '/' + str(j)
imgs = os.listdir(curFolderPath)
for img in imgs:
curImg = curFolderPath + '/' + img
if curImg[-3:] == 'png':
img = Image.open(curImg,'r')
img = img.resize((28, 28))
savedImg = img
imgdata = (255-np.array(img.getdata()))/255
USPSMat.append(imgdata)
USPSTar.append(j)
def reformat(labels):
labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32)
return labels
USPSMat = np.array(USPSMat)
print(USPSMat.shape)
USPSTar= np.array(USPSTar)
print(reformat(USPSTar).shape)
test = np_utils.to_categorical(np.array(USPSTar),10)
# In[ ]:
SVM_prediction_usps=model_fit.predict(USPSMat)
classifier.score(USPSMat,USPSTar)
# # Confusion matrix of USPS test dataset
# In[ ]:
conf_mat1 = confusion_matrix(USPSTar, SVM_prediction_usps)
plot_confusion_matrix(cm = conf_mat1,
normalize = False,
title = "Confusion Matrix")
# In[ ]:
from sklearn.metrics import classification_report
print(classification_report(SVM_prediction_usps,
USPSTar))