-
Notifications
You must be signed in to change notification settings - Fork 0
/
SVM.py
122 lines (102 loc) · 6.07 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
import numpy as np
import matplotlib.pyplot as plt
from skimage import io
from sklearn import svm
from sklearn.metrics import accuracy_score, confusion_matrix, ConfusionMatrixDisplay
"""
Function that normalizes the train and test data by using the standard method.
The normalization is done by subtracting the mean of the attributes of the
training data and dividing the result by the standard deviation.
Part of this function was taken from lab 4.
"""
def normalizeData(trainData, testData):
mean = np.mean(trainData, axis=0)
std = np.std(trainData, axis=0)
trainData = trainData-mean
trainData = trainData/std
testData = testData-mean
testData = testData/std
return trainData, testData
"""
Function that reads the train and validation data and converts them to nparrays.
"""
def readTrainAndValidationData():
f = open("./data/train.txt")
aux = f.readline() # Reading the header of the file.
trainImagesOriginal = []
trainLabelsOriginal = []
for line in f:
aux = line.strip().split(",") # Getting the label and the class of the image.
img = io.imread("./data/train+validation/{}".format(aux[0])) # Reading the features of the image.
dataImage = np.asarray(img).reshape(-1) # Converting the image to an 1D nparray, by flattening the 16x16x3 matrix.
trainImagesOriginal.append(dataImage)
trainLabelsOriginal.append(int(aux[1]))
f.close()
trainImages = np.array(trainImagesOriginal).astype('float64') # Creating the train images nparray.
trainLabels = np.array(trainLabelsOriginal) # Creating the train labels nparray.
f = open("./data/validation.txt")
aux = f.readline() # Reading the header of the file.
validationImagesOriginal = []
validationLabelsOriginal = []
for line in f:
aux = line.strip().split(",") # Getting the id and the class of the image.
img = io.imread("./data/train+validation/{}".format(aux[0])) # Reading the features of the image.
dataImage = np.asarray(img).reshape(-1) # Converting the image to an 1D nparray, by flattening the 16x16x3 matrix.
validationImagesOriginal.append(dataImage)
validationLabelsOriginal.append(int(aux[1]))
f.close()
validationImages = np.array(validationImagesOriginal).astype('float64') # Creating the validation images nparray.
validationLabels = np.array(validationLabelsOriginal) # Creating the validation labels nparray.
return trainImages, trainLabels, validationImages, validationLabels
"""
Function that reads the test data and converts them to a nparray.
"""
def readTestData():
f = open("./data/test.txt")
aux = f.readline() # Reading the header of the file.
testImagesOriginal = []
imagesName = []
for line in f:
aux = line.strip() # Getting the id and the class of the image.
img = io.imread("./data/test/{}".format(aux)) # Reading the features of the image.
dataImage = np.asarray(img).reshape(-1) # Converting the image to an 1D nparray, by flattening the 16x16x3 matrix.
testImagesOriginal.append(dataImage)
imagesName.append(aux) # Saving the id of the image because it is used when writing the predicted labels
f.close() # in the output file.
testImages = np.array(testImagesOriginal).astype('float64') # Creating the test images nparray.
return testImages, imagesName
"""
Function that normalizes the train and validation data and predicts the labels on the validation data.
This function is used to measure the accuracy of the classifier on the validation data.
"""
def normalizeAndPredictOnValidationData(trainImages, trainLabels, validationImages, validationLabels):
trainImages, validationImages = normalizeData(trainImages, validationImages)
model = svm.SVC(C=3, kernel="poly") # Initializing the model. The parameters were chosen by trial and error.
model.fit(trainImages, trainLabels) # Training the model.
predictedLabels = model.predict(validationImages) # Making predictions on the validations data.
print("Accuracy on validation data:", accuracy_score(validationLabels, predictedLabels))
confMatrix = confusion_matrix(validationLabels, predictedLabels, labels=model.classes_) # Displaying the confusion matrix.
display = ConfusionMatrixDisplay(confMatrix, display_labels=model.classes_)
display.plot()
plt.show()
"""
Function that normalizes the train and test data and predicts the labels on the test data.
This function also creates the output file.
"""
def normalizeAndPredictOnTestData(trainImages, trainLabels, testImages, imagesName):
trainImages, testImages = normalizeData(trainImages, testImages)
model = svm.SVC(C=5, kernel="rbf") # Initializing the model. The parameters were chosen by trial and error.
model.fit(trainImages, trainLabels) # Training the model.
predictedLabels = model.predict(testImages) # Making predictions on the test data.
f = open("predictionsSVM.txt",'w')
f.write("id,label\n")
for i in range(0,len(predictedLabels)):
f.write(imagesName[i] + ',' + str(predictedLabels[i]) + '\n') # Writing the id and the predicted label
f.close() # of each picture in the test data set.
# Getting the data.
trainImages, trainLabels, validationImages, validationLabels = readTrainAndValidationData()
testImages, imagesName = readTestData()
# For getting the predictions on the validation data, uncomment the next line.
normalizeAndPredictOnValidationData(trainImages, trainLabels, validationImages, validationLabels)
# For getting the predictions on the test data and creating the output file, uncomment the next line.
# normalizeAndPredictOnTestData(trainImages, trainLabels, testImages, imagesName)