-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiClassKernelPerceptron.py
More file actions
43 lines (33 loc) · 1.93 KB
/
Copy pathmultiClassKernelPerceptron.py
File metadata and controls
43 lines (33 loc) · 1.93 KB
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
from kernelPerceptron import KernelPerceptron, polynomialKernel
import pandas as pd
import numpy as np
# One vs all multi class kernel perceptron
class MultiClassKernelPerceptron():
def __init__(self, epochNumber, polynomialDegree):
self.epochNumber = epochNumber
self.polynomialDegree = polynomialDegree
self.perceptrons = []
def train(self, xTrain, yTrain):
# Creating one perceptron for each unique label (10 digits)
for label in np.unique(yTrain):
self.perceptrons.append(KernelPerceptron(label, self.epochNumber, self.polynomialDegree))
# Kernel matrix is the same for all classes so it is calculated once and pass around
kernelTrain = pd.read_csv("k{0}.csv".format(self.polynomialDegree), header=None)
kernelTrain = np.reshape(kernelTrain.values, (10000,10000))
# Training models (10 binary classifiers)
for x in self.perceptrons:
x.train(xTrain.values, yTrain.values, kernelTrain)
def predict(self, xTest, yTest):
interim = int(self.epochNumber/5)
perceptronPredictions = np.zeros((2,interim,len(xTest),len(self.perceptrons)))
# Each model gives the certainty that an image belongs to its class
for i, perceptron in enumerate(self.perceptrons):
perceptronPredictions[:,:,:,i] = perceptron.predict(xTest, yTest)
maxPrediction = np.zeros((2,len(xTest),interim))
# Index/label of perceptron with max certainty
for version in range(2):
for inter in range(interim):
maxPrediction[version,:,inter] = np.argmax(perceptronPredictions[version,inter,:,:], axis = 1)
# Return class label for most accurate prediction of perceptron for each image
return np.array([[self.perceptrons[int(a)].classLabel for a in maxPrediction[i,:,j]]
for j in range(maxPrediction.shape[2]) for i in range(maxPrediction.shape[0])])