#-------------------------------------------------------------------------
# AUTHOR: Haowen Yin
# FILENAME: svm.py
# SPECIFICATION: Implement support vector machine
# FOR: CS 4200- Assignment #3
# TIME SPENT: 40 min
#-----------------------------------------------------------*/

#IMPORTANT NOTE: DO NOT USE ANY ADVANCED PYTHON LIBRARY TO COMPLETE THIS CODE SUCH AS numpy OR pandas. You have to work here only with standard vectors and arrays

#importing some Python libraries
from sklearn import svm
import csv

dbTraining = []
dbTest = []
X_training = []
Y_training = []
c = [1, 5, 10, 100]
degree = [1, 2, 3]
kernel = ["linear", "poly", "rbf"]
decision_function_shape = ["ovo", "ovr"]
highestAccuracy = 0

#reading the data in a csv file
with open('optdigits.tra', 'r') as trainingFile:
  reader = csv.reader(trainingFile)
  for i, row in enumerate(reader):
      X_training.append(row[:-1])
      Y_training.append(row[-1])

#reading the data in a csv file
with open('optdigits.tes', 'r') as testingFile:
  reader = csv.reader(testingFile)
  for i, row in enumerate(reader):
      dbTest.append (row)

#created 4 nested for loops that will iterate through the values of c, degree, kernel, and decision_function_shape
#--> add your Python code here

for i in c: #iterates over c
    for j in degree: #iterates over degree
        for k in kernel : #iterates kernel
           for z in decision_function_shape: #iterates over decision_function_shape

                #Create an SVM classifier that will test all combinations of c, degree, kernel, and decision_function_shape as hyperparameters. For instance svm.SVC(c=1)
                clf = svm.SVC(C=i,degree=j,kernel=k,decision_function_shape=z)

                #Fit Random Forest to the training data
                clf.fit(X_training, Y_training)

                #make the classifier prediction for each test sample and start computing its accuracy
                #--> add your Python code here
                accuracy = 0
                for instance in dbTest:
                    class_predicted = clf.predict([instance[:-1]])
                    true_label = instance[-1]
                    if(class_predicted[0] == true_label):
                        accuracy += 1
                
                accuracy /= len(dbTest)

                #check if the calculated accuracy is higher than the previously one calculated. If so, update 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(accuracy > highestAccuracy):
                    highestAccuracy = accuracy
                    print("Highest SVM accuracy so far: ", highestAccuracy, "Parameters: ", "c = ",i,", degree = ",j,", kernal = ",k,", decision_function_shape = ",z)

#print the final, highest accuracy found together with the SVM hyperparameters
#Example: "Highest SVM accuracy: 0.95, Parameters: a=10, degree=3, kernel= poly, decision_function_shape = 'ovr'"
#--> add your Python code here

#print("Highest SVM accuracy so far: ", highestAccuracy, "Parameters: ", "c = ",i,", degree = ",j,", kernal = ",k,", decision_function_shape = ",z)












