-
Notifications
You must be signed in to change notification settings - Fork 0
/
SVM
83 lines (66 loc) · 2.59 KB
/
SVM
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
import numpy as np
import os
from sklearn import svm
from PIL import Image
def load_data(path, index):
path = path + str(index)
counter = 0
images = np.array([[]])
for root,dirs,files in os.walk(path):
for f in files:
abs_path = os.path.join(root,f)
image = Image.open(abs_path)
image_array = np.array(image)
# flatten to 1-D vector; image_f: 784
image_f = image_array.flatten()
# add the new flattened image to our images set for training
if counter == 0:
images = np.concatenate((images, np.array([image_f])),axis=1)
else:
images = np.concatenate((images, np.array([image_f])))
# images' shape: (num_img, 784) # (1, num_img*784)
counter += 1
print(images.shape)
return images, counter # images: [num_image, 784]
def SVM(test_file_name):
train_images = np.array([[]])
train_label = np.array([])
for i in range(0,10):
label = i
images, counter = load_data("D:\\HW4\\dataset\\train\\",i)
if i == 0:
train_images = images
else:
train_images = np.concatenate((train_images, images))
# train_images' shape: (total_train_img, 784)
train_label_i = np.array([label for j in range(0, counter)])
train_label = np.append(train_label, train_label_i)
print("label %d has been loaded"%(i))
test_images = np.array([[]])
test_label = np.array([])
for i in range(0,10):
label = i
images, counter = load_data(test_file_name,i)
if i == 0:
test_images = images
else:
test_images = np.concatenate((test_images, images))
# test_images' shape: (total_test_img, 784)
test_label_i = np.array([label for j in range(0, counter)])
test_label = np.append(test_label, test_label_i)
print("Data loaded successfully")
print(train_images.shape) # output: (60000, 784)
print(test_images.shape) # output: (10000, 784)
counter = 0
error_times = 0
lsvc = svm.LinearSVC(C=0.001,max_iter=10000,dual=False)
lsvc.fit(train_images, train_label)
predict_label = lsvc.predict(test_images)
for label in predict_label:
print("Test "+str(counter+1)+": Predict: "+str(label)+" "+"True: "+str(test_label[counter]))
if label != test_label[counter]:
error_times += 1
counter += 1
return error_times/counter
error_rate = SVM("D:\\HW4\\dataset\\test\\")
print("The error rate is %f"%(error_rate))