Created
October 5, 2019 03:03
-
-
Save Shashi18/7215d46b77b3ed512f82b76c769e797d to your computer and use it in GitHub Desktop.
This is a code for multiclass perceptron.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as nmpy #Importing Numpy library for matrix operation | |
#Creating a Perceptron Class with training and pocket algorithm testing function | |
class Create_Perceptron(): | |
def __init__(self, vector, weight, label, learning, bias): | |
self.vector = vector | |
self.weight = weight | |
self.labels = label | |
self.current_accuracy = 0 | |
self.pocket_weight = weight | |
self.learning = learning | |
self.bias = bias | |
def train(self): | |
y = 0 | |
for k in range(0, 250): | |
for i in range(0,len(self.labels)): | |
accuracy = self.test(self.vector, self.weight, self.labels) | |
if(accuracy >= self.current_accuracy): | |
self.pocket_weight = self.weight | |
self.current_accuracy = accuracy | |
y = nmpy.dot(nmpy.transpose(self.weight),nmpy.transpose(nmpy.array([self.vector[i]]))) + self.bias | |
if(y >= 0): | |
y = 1 | |
else: | |
y = 0 | |
self.bias += (self.labels[i]-y)*self.learning | |
self.weight = self.weight + (self.labels[i]-y)*self.learning*self.vector[i].reshape(4, 1) | |
#print(self.current_accuracy) | |
def test(self, test_vector, weights, labels): | |
correct = 0 | |
for i in range(0,len(labels)): | |
y = nmpy.dot(nmpy.transpose(weights),nmpy.transpose(nmpy.array([test_vector[i]]))) | |
if(y >= 0): | |
y = 1 | |
else: | |
y = 0 | |
if(y == labels[i]): | |
correct += 1 | |
return correct/len(labels)*100 | |
def pocket_weight(self): | |
return self.pocket_weight | |
def bias(self): | |
return self.bias | |
def export_data(output_file, inp, vector): | |
arr = vector.split(',') | |
string = arr[0]+','+arr[1]+','+arr[2]+','+arr[3] | |
if(inp==2): | |
output_file.write((string+',Iris-setosa\n')) | |
elif(inp==0): | |
output_file.write((string+',Iris-versicolor\n')) | |
else: | |
output_file.write((string+',Iris-virginica\n')) | |
#Open the test data | |
#\ used to escape U from Users as it is a unicode character | |
file = open('C:\\Users\Shashi Suman\Desktop\Queens U\CISC 874\Assignment 1 ANN\iris_train.txt') | |
#Read the first line | |
line = file.readline() | |
label = [] | |
weight = [] | |
#Spliting the read line into array | |
arr = line.split(',') | |
#Creating 3 input sets for {Setosa, Versicolor}, {Versicolor, Virginica}, {Virginica, Setosa} | |
x1, x2, x3 = nmpy.empty((0, 4), dtype=float), nmpy.empty((0, 4), dtype=float), nmpy.empty((0, 4), dtype=float) | |
#Creating Label Vector | |
label_1, label_2, label_3 = nmpy.empty((0, 1)), nmpy.empty((0, 1)), nmpy.empty((0, 1)) | |
#Reading each line | |
while line: | |
arr = line.split(',') | |
if(arr[4].strip()=='Iris-setosa'): | |
x1 = nmpy.append(x1, nmpy.array([[arr[0].strip(),arr[1].strip(),arr[2].strip(),arr[3].strip()]]), 0) | |
x3 = nmpy.append(x3, nmpy.array([[arr[0].strip(),arr[1].strip(),arr[2].strip(),arr[3].strip()]]) ,0) | |
if(label_1.size != 0): | |
label_1 = nmpy.append(label_1, nmpy.array([[0]]), 0) | |
else: | |
label_1 = nmpy.array([[0]]) | |
if(label_3.size != 0): | |
label_3 = nmpy.append(label_3, nmpy.array([[1]]), 0) | |
else: | |
label_3 = nmpy.array([[1]]) | |
elif(arr[4].strip()=='Iris-virginica'): | |
x3 = nmpy.append(x3, nmpy.array([[arr[0].strip(),arr[1].strip(),arr[2].strip(),arr[3].strip()]]), 0) | |
x2 = nmpy.append(x2, nmpy.array([[arr[0].strip(),arr[1].strip(),arr[2].strip(),arr[3].strip()]]), 0) | |
if(label_3.size != 0): | |
label_3 = nmpy.append(label_3, nmpy.array([[0]]), 0) | |
else: | |
label_3 = nmpy.array([[0]]) | |
if(label_2.size != 0): | |
label_2 = nmpy.append(label_2, nmpy.array([[1]]), 0) | |
else: | |
label_2 = nmpy.array([[1]]) | |
elif(arr[4].strip()=='Iris-versicolor'): | |
x1 = nmpy.append(x1, nmpy.array([[arr[0].strip(),arr[1].strip(),arr[2].strip(),arr[3].strip()]]), 0) | |
x2 = nmpy.append(x2, nmpy.array([[arr[0].strip(),arr[1].strip(),arr[2].strip(),arr[3].strip()]]), 0) | |
if(label_1.size != 0): | |
label_1 = nmpy.append(label_1, nmpy.array([[1]]), 0) | |
else: | |
label_1 = nmpy.array([[1]]) | |
if(label_3.size != 0): | |
label_2 = nmpy.append(label_2, nmpy.array([[0]]), 0) | |
else: | |
label_2 = nmpy.array([[0]]) | |
#print(len(x2),' ',len(label_2)) | |
line = file.readline() | |
#Set Learning Rate | |
lr = 0.5 | |
#Initialize weights with zeros | |
weight = nmpy.zeros([4, 1], dtype=float) | |
#Changing the data type of Input sets | |
x1 = x1.astype('float32') | |
x2 = x2.astype('float32') | |
x3 = x3.astype('float32') | |
#Changing the datatype of label sets | |
label_1 = label_1.astype('float32') | |
label_2 = label_2.astype('float32') | |
label_3 = label_3.astype('float32') | |
#Creating 3 Perceptron for each set. Passing input,weight, label, learning rate and bias | |
percep_1 = Create_Perceptron(x1, weight, label_1, lr, 0) | |
percep_2 = Create_Perceptron(x2, weight, label_2, lr, 40) | |
percep_3 = Create_Perceptron(x3, weight, label_3, lr, 0) | |
#Training begins for each instantiated perceptron | |
percep_1.train() | |
percep_2.train() | |
percep_3.train() | |
#Close the opened file | |
file.close() | |
#Open the test file | |
test_file = open('C:\\Users\Shashi Suman\Desktop\Queens U\CISC 874\Assignment 1 ANN\iris_test.txt') | |
#Open the Output File with write permission | |
output_file = open('C:\\Users\Shashi Suman\Desktop\Queens U\CISC 874\Assignment 1 ANN\\valid_output.txt','w') | |
#read the first line | |
line = test_file.readline() | |
#Accuracy Set to zero | |
accuracy_rate = 0; | |
length = 0 | |
# Common Confusion Matrix | |
confusion = nmpy.zeros((4, 4)) | |
confusion[0, 1] = 100 #Predicted Iris Setosa | |
confusion[0, 2] = 101 #Predicted Iris Versicolor | |
confusion[0, 3] = 110 #Predicted Iris Virginica | |
confusion[1, 0] = -100 #Actual Iris Setosa | |
confusion[2, 0] = -101 #Actual Iris Versicolor | |
confusion[3, 0] = -110 #Actual Iris Virginica | |
#TP-True Positive | |
TP = 0 | |
#TN-True Negative | |
FN = 0 | |
#FP-False Positive | |
FP = 0 | |
TN = 0 | |
Setosa_FN = 0 | |
Setosa_TP = 0 | |
Versicolor_FN = 0 | |
Versicolor_TP = 0 | |
Virginica_FN = 0 | |
Virginica_TP = 0 | |
Setosa_FP = 0 | |
Versicolor_FP = 0 | |
Virginica_FP = 0 | |
#Reading Line from test data | |
while line: | |
arr = line.split(',') | |
test_vector = nmpy.array([[arr[0].strip(), arr[1].strip(), arr[2].strip(), arr[3].strip()]]).astype('float32') | |
#Dot Product for each set with trained Weights | |
f1 = nmpy.dot(nmpy.transpose(percep_1.pocket_weight),nmpy.transpose(test_vector)) + percep_1.bias | |
f2 = nmpy.dot(nmpy.transpose(percep_2.pocket_weight),nmpy.transpose(test_vector)) + percep_2.bias | |
f3 = nmpy.dot(nmpy.transpose(percep_3.pocket_weight),nmpy.transpose(test_vector)) + percep_3.bias | |
var = nmpy.argmax(nmpy.array([f1, f2, f3])) | |
######Confusion Matrix Design########## | |
if(var==2 and arr[4].strip()=='Iris-setosa'): | |
TP += 1 | |
TN += 1 | |
Setosa_TP += 1 | |
confusion[1,1] += 1 | |
elif(var==2 and arr[4].strip()=='Iris-versicolor'): | |
confusion[2, 1] += 1 | |
FP += 1 | |
FN += 1 | |
elif(var==2 and arr[4].strip()=='Iris-virginica'): | |
confusion[3, 1] += 1 | |
FP += 1 | |
FN += 1 | |
elif(var==0 and arr[4].strip()=='Iris-versicolor'): | |
TP += 1 | |
TN += 1 | |
Versicolor_TP += 1 | |
confusion[2,2] += 1 | |
elif(var==0 and arr[4].strip()=='Iris-setosa'): | |
confusion[1, 2] += 1 | |
FP += 1 | |
FN += 1 | |
elif(var==0 and arr[4].strip()=='Iris-virginica'): | |
confusion[3, 2] += 1 | |
FP += 1 | |
FN += 1 | |
elif(var==1 and arr[4].strip()=='Iris-virginica'): | |
TP += 1 | |
TN += 1 | |
confusion[3,3] += 1 | |
Virginica_TP += 1 | |
elif(var==1 and arr[4].strip()=='Iris-versicolor'): | |
confusion[2, 3] += 1 | |
FP += 1 | |
FN += 1 | |
elif(var==1 and arr[4].strip()=='Iris-setosa'): | |
confusion[1, 3] += 1 | |
FP += 1 | |
FN += 1 | |
#################################################### | |
output_file.write('Iris-setosa\n') | |
#Output the data to file | |
if(var==2): | |
print('Iris-setosa') | |
export_data(output_file,var,line) | |
if(arr[4].strip()=='Iris-setosa'): | |
accuracy_rate += 1 | |
elif(var==0): | |
print('Iris-versicolor') | |
export_data(output_file,var,line) | |
if(arr[4].strip()=='Iris-versicolor'): | |
accuracy_rate += 1 | |
else: | |
print('Iris-virginica') | |
export_data(output_file,var,line) | |
if(arr[4].strip()=='Iris-virginica'): | |
accuracy_rate += 1 | |
line = test_file.readline() | |
length += 1 | |
#Updating Each Class TP (True Positive), FP (False Positive), FN (False Negative) | |
Setosa_FP = confusion[2, 1] + confusion[3, 1] | |
Versicolor_FP = confusion[1, 2] + confusion[3, 2] | |
Virginica_FP = confusion[1, 3] + confusion[2, 3] | |
Setosa_FN = confusion[1, 2] + confusion[1, 3] | |
Versicolor_FN = confusion[2, 1] + confusion[2, 3] | |
Virginica_FN = confusion[3, 1] + confusion[3, 2] | |
#Print the Confursion Matrix, Precision and Recall | |
print('\n##### Confusion Matrix_Testing Data #####') | |
print('100 --> Predicted Setosa 101 --> Predicted Versicolor 110 --> Predicted Virginica') | |
print('-100 --> Real Setosa -101 --> Real Versicolor -110 --> Real Virginica') | |
print(confusion) | |
print('\nThe Accuracy is: ',accuracy_rate/length*100) | |
print('The Overall Matrix Accuracy is: ',(TP+TN)/(TP+TN+FP+FN)*100) | |
print('\nRecall (Iris_Setosa)',Setosa_TP*100/(Setosa_TP + Setosa_FN)) | |
print('Precision (Iris_Setosa)',Setosa_TP*100/(Setosa_TP + Setosa_FP)) | |
print('\nRecall (Iris_Versicolor)',Versicolor_TP*100/(Versicolor_TP + Versicolor_FN)) | |
print('Precision (Iris_Versicolor)',Versicolor_TP*100/(Versicolor_TP + Versicolor_FP)) | |
print('\nRecall (Iris_Virginica)',Virginica_TP*100/(Virginica_TP + Virginica_FN)) | |
print('Precision (Iris_Virginica)',Virginica_TP*100/(Virginica_TP + Virginica_FP)) | |
print('#########################################################\n') | |
test_file.close() | |
output_file.close() | |
#ReOpen Training data for confusion matrix design | |
file = open('C:\\Users\Shashi Suman\Desktop\Queens U\CISC 874\Assignment 1 ANN\iris_train.txt') | |
line = file.readline() | |
#Initalize Confusion matrix with 4 X 4 zeros | |
confusion = nmpy.zeros((4, 4),dtype=int) | |
confusion = nmpy.zeros((4, 4)) | |
confusion[0, 1] = 100 #Predicted Iris Setosa | |
confusion[0, 2] = 101 #Predicted Iris Versicolor | |
confusion[0, 3] = 110 #Predicted Iris Virginica | |
confusion[1, 0] = -100 #Actual Iris Setosa | |
confusion[2, 0] = -101 #Actual Iris Versicolor | |
confusion[3, 0] = -110 #Actual Iris Virginica | |
TP = 0 | |
FN = 0 | |
FP = 0 | |
TP = 0 | |
TN = 0 | |
Setosa_FN = 0 | |
Setosa_TP = 0 | |
Versicolor_FN = 0 | |
Versicolor_TP = 0 | |
Virginica_FN = 0 | |
Virginica_TP = 0 | |
Setosa_FP = 0 | |
Versicolor_FP = 0 | |
Virginica_FP = 0 | |
while line: | |
arr = line.split(',') | |
test_vector = nmpy.array([[arr[0].strip(), arr[1].strip(), arr[2].strip(), arr[3].strip()]]).astype('float32') | |
#Dor Product for each set | |
f1 = nmpy.dot(nmpy.transpose(percep_1.pocket_weight),nmpy.transpose(test_vector)) | |
f2 = nmpy.dot(nmpy.transpose(percep_2.pocket_weight),nmpy.transpose(test_vector)) | |
f3 = nmpy.dot(nmpy.transpose(percep_3.pocket_weight),nmpy.transpose(test_vector)) | |
#Finding the maximum with dot products | |
var = nmpy.argmax(nmpy.array([f1, f2, f3])) | |
######Confusion Matrix Design########## | |
if(var==2 and arr[4].strip()=='Iris-setosa'): | |
TP += 1 | |
TN += 1 | |
Setosa_TP += 1 | |
confusion[1,1] += 1 | |
elif(var==2 and arr[4].strip()=='Iris-versicolor'): | |
confusion[2, 1] += 1 | |
FP += 1 | |
FN += 1 | |
elif(var==2 and arr[4].strip()=='Iris-virginica'): | |
confusion[3, 1] += 1 | |
FP += 1 | |
FN += 1 | |
elif(var==0 and arr[4].strip()=='Iris-versicolor'): | |
TP += 1 | |
TN += 1 | |
Versicolor_TP += 1 | |
confusion[2,2] += 1 | |
elif(var==0 and arr[4].strip()=='Iris-setosa'): | |
confusion[1, 2] += 1 | |
FP += 1 | |
FN += 1 | |
elif(var==0 and arr[4].strip()=='Iris-virginica'): | |
confusion[3, 2] += 1 | |
FP += 1 | |
FN += 1 | |
elif(var==1 and arr[4].strip()=='Iris-virginica'): | |
TP += 1 | |
TN += 1 | |
confusion[3,3] += 1 | |
Virginica_TP += 1 | |
elif(var==1 and arr[4].strip()=='Iris-versicolor'): | |
confusion[2, 3] += 1 | |
FP += 1 | |
FN += 1 | |
elif(var==1 and arr[4].strip()=='Iris-setosa'): | |
confusion[1, 3] += 1 | |
FP += 1 | |
FN += 1 | |
#################################################### | |
if(var==2): | |
#print('Iris-setosa') | |
#export_data(var,line) | |
if(arr[4].strip()=='Iris-setosa'): | |
accuracy_rate += 1 | |
elif(var==0): | |
#print('Iris-versicolor') | |
#export_data(var,line) | |
if(arr[4].strip()=='Iris-versicolor'): | |
accuracy_rate += 1 | |
else: | |
#print('Iris-virginica') | |
#export_data(var,line) | |
if(arr[4].strip()=='Iris-virginica'): | |
accuracy_rate += 1 | |
line = file.readline() | |
length += 1 | |
#Updating the TP,FP, FN for each class | |
Setosa_FP = confusion[2, 1] + confusion[3, 1] | |
Versicolor_FP = confusion[1, 2] + confusion[3, 2] | |
Virginica_FP = confusion[1, 3] + confusion[2, 3] | |
Setosa_FN = confusion[1, 2] + confusion[1, 3] | |
Versicolor_FN = confusion[2, 1] + confusion[2, 3] | |
Virginica_FN = confusion[3, 1] + confusion[3, 2] | |
#Printing the confusion matrix, recall, precision for each class | |
print('Updated Weights') | |
print(percep_1.weight) | |
print(percep_2.weight) | |
print(percep_3.weight) | |
print('\n##########################################################################') | |
print('\n#### Confusion Matrix Training Data ####') | |
print('100 --> Predicted Setosa 101 --> Predicted Versicolor 110 --> Predicted Virginica') | |
print('-100 --> Real Setosa -101 --> Real Versicolor -110 --> Real Virginica') | |
print(confusion) | |
print('\nThe Accuracy is: ',accuracy_rate/length*100) | |
print('The Overall Matrix Accuracy is: ',(TP+TN)/(TP+TN+FP+FN)*100) | |
print('\nRecall (Iris_Setosa)',Setosa_TP*100/(Setosa_TP + Setosa_FN)) | |
print('Precision (Iris_Setosa)',Setosa_TP*100/(Setosa_TP + Setosa_FP)) | |
print('\nRecall (Iris_Versicolor)',Versicolor_TP*100/(Versicolor_TP + Versicolor_FN)) | |
print('Precision (Iris_Versicolor)',Versicolor_TP*100/(Versicolor_TP + Versicolor_FP)) | |
print('\nRecall (Iris_Virginica)',Virginica_TP*100/(Virginica_TP + Virginica_FN)) | |
print('Precision (Iris_Virginica)',Virginica_TP*100/(Virginica_TP + Virginica_FP)) | |
print('##################') | |
file.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment