Skip to content

Implementation of the Perceptron algorithm for binary and multi-class classification

Notifications You must be signed in to change notification settings

Sami-I/Perceptron

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

INSTRUCTIONS TO RUN CODE

To run the .py file compile it as you would with any other .py file using an IDE or terminal. However, test.data and train.data must be in the same folder as the .py file.

To randomise the datasets to produce the results in the pdf document, uncomment this code in the .py file
#random.shuffle(training_data)
#random.shuffle(test_data)

For question 3, to train data to discriminate between any 2 classes using the binary perceptron you will need to call the following function: perceptronTrain(trainingData,maxIter,class1,class2)
this will return a tuple of (bias,WeightVector) which you then pass into perceptronAccuracy(data,bias,weightVector,class1,class2) with the training/test data and the same 2 classes that you trained on as arguments - running this function returns the accuracy of discriminating between class1 and class2.
For example to get train classification accuracies for class 1 and class 3 after training for 20 iterations, you would first run: perceptronTrain(training_data,20,"class-1","class-3") NOTE: The 2 labels given as arguments have to be passed exactly as they are in the actual data sets so: "class-1", "class-2", or "class-3". This would then return a tuple: (bias,weightVector). Then to get the training classification accuracies for class 1 and class 3 you would run perceptronAccuracy(training_data,bias,weightVector,"class-1","class-3") to get the accuracy of the binary perceptron when discriminating between class 1 and class 3.

For question 4, there are 2 functions that implement the one vs rest approach. The first: calculatePredictionValues(data,maxIter) that returns the prediction models for each class. So for question 4, to get the prediction values for each class after training for 20 iterations you would run: predictionModels = calculatePredictionValues(training_data,20). Then to actually apply the multi-class classification perceptron algorithm: you would call multiClassPerceptron(data,predictionModels) where data is either training_data or test_data to get accuracies for training and test data.

For question 5, there are 3 additional functions that implement the multi-class classifier with an l2 term. The first is: l2perceptronTrain(trainingData,maxIter,l2term,class1,class2) that is the same as the perceptronTrain function but takes an additional argument: l2term and updates the weight vector using this term: weightVector = (1-2*l2term)*weightVector + classLabel*features. The 2nd function: calculatel2predictionValues(data,maxIter,l2term) is the same as the calculatePredictionValues(data,maxIter) function but again takes an l2term as an argument and uses this value to calculate the prediction model for each class label: classPredictionModel[classLabel] = l2perceptronTrain(data, maxIter, l2term, classLabel, ""). Finally, l2multiClassPerceptron(trainingData,testData,maxIter,l2terms) puts these functions together and calculates the accuracies using each l2 term in l2terms and also outputs the l2 term that gives the best accuracy with trainingData as the data to train the multi-class classifier with the l2term on and testData to test the multi-class classifier with the l2 term on.

About

Implementation of the Perceptron algorithm for binary and multi-class classification

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages