-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvm.py
74 lines (56 loc) · 3.52 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
#-------------------------------------------------------------------------
# AUTHOR: Jeremiah Garcia
# FILENAME: svm.py
# SPECIFICATION: This program executes the SVM model.
# FOR: CS 4210- Assignment #3
# TIME SPENT: 30m
#-----------------------------------------------------------*/
#IMPORTANT NOTE: YOU HAVE TO WORK WITH THE PYTHON LIBRARIES numpy AND pandas to complete this code.
#importing some Python libraries
from sklearn import svm
import numpy as np
import pandas as pd
#defining the hyperparameter values
c = [1, 5, 10, 100]
degree = [1, 2, 3]
kernel = ["linear", "poly", "rbf"]
decision_function_shape = ["ovo", "ovr"]
df = pd.read_csv('optdigits.tra', sep=',', header=None) #reading the training data by using Pandas library
X_training = np.array(df.values)[:,:64] #getting the first 64 fields to create the feature training data and convert them to NumPy array
y_training = np.array(df.values)[:,-1] #getting the last field to create the class training data and convert them to NumPy array
df = pd.read_csv('optdigits.tes', sep=',', header=None) #reading the training data by using Pandas library
X_test = np.array(df.values)[:,:64] #getting the first 64 fields to create the feature testing data and convert them to NumPy array
y_test = np.array(df.values)[:,-1] #getting the last field to create the class testing data and convert them to NumPy array
#created 4 nested for loops that will iterate through the values of c, degree, kernel, and decision_function_shape
#--> add your Python code here
highest_accuracy = 0
for see in c:
for deg in degree:
for kern in kernel:
for decision in decision_function_shape:
#Create an SVM classifier that will test all combinations of c, degree, kernel, and decision_function_shape.
#For instance svm.SVC(c=1, degree=1, kernel="linear", decision_function_shape = "ovo")
#--> add your Python code here
clf = svm.SVC(C=see, degree=deg, kernel=kern, decision_function_shape=decision)
#Fit SVM to the training data
#--> add your Python code here
clf.fit(X_training, y_training)
#make the SVM prediction for each test sample and start computing its accuracy
#hint: to iterate over two collections simultaneously, use zip()
#Example. for (x_testSample, y_testSample) in zip(X_test, y_test):
#to make a prediction do: clf.predict([x_testSample])
#--> add your Python code here
total_pred = 0
correct_pred = 0
for x_testSample, y_testSample in zip(X_test,y_test):
pred = clf.predict([x_testSample])
total_pred +=1
if pred[0] == y_testSample:
correct_pred += 1
acc = correct_pred/total_pred
#check if the calculated accuracy is higher than the previously one calculated. If so, update the highest accuracy and print it together
#with the SVM hyperparameters. Example: "Highest SVM accuracy so far: 0.92, Parameters: a=1, degree=2, kernel= poly, decision_function_shape = 'ovo'"
#--> add your Python code here
if acc > highest_accuracy:
highest_accuracy = acc
print(f"Highest SVM accuracy so far:{acc}, Parameters: C = {see}, degree = {deg}, kernel = {kern}, decision_function_shape = {decision}")