The perceptron is a linear classifier, therefore it will never get to the state with all the input vectors classified correctly if the training set D is not linearly separable, i.e. if the positive examples can not be separated from the negative examples by a hyperplane. (Source: Wikipedia)

1.linear classifier for class C1 and C2

2. linear classifier for class C2 and C3

Sample output:-

[zytham@s158519-vm perceptron]$ python Perceptron.py

1. Run C1C2 classifier

2. Run C2C3 classifier

Enter your choice: 1

[zytham@s158519-vm perceptron]$ python Perceptron.py

1. Run C1C2 classifier

2. Run C2C3 classifier

Enter your choice:

2

**Sample Data**: C1, C2 and C3 represents three different class of data. It is guaranteed that these data set are linearly separable.Linearly separable data sample (Three classes C1, C2 and C3) |

**Problem statement**:- Write a program to generate1.linear classifier for class C1 and C2

2. linear classifier for class C2 and C3

**Sample code**: -import sys import matplotlib.pyplot as plt import numpy as np # Make a prediction with weights def compute(row, weights): bias = weights[2] output = bias #output = (w1 * X1) + (w2 * X2) + bias for i in range(len(row)-1): output += weights[i] * row[i] ##print "output is",output return 1 if output > 0 else 0 #extrapolate classifer line with same slope as computed by final weights def getMultiplePoints(x,y,weight,boundX1,boundX2): x1 =[x,0] x2 =[0,y] pointsX = [] pointsY = [] pointsX.insert(1,y) pointsX.insert(2,0) pointsY.insert(1,0) pointsY.insert(2,x) #for boundX1 pointsX.insert(0,boundX1) temp = -(weight[0]*boundX1 + weight[2])/weight[1] pointsY.insert(0,temp) #for boundX2 pointsX.insert(3,boundX2) temp = -((weight[0]*boundX2) + weight[2])/weight[1] pointsY.insert(3,temp) return (pointsX,pointsY) #plot points def plotCoordinates(dataset,weightPlot): XList1 =[] YList1 =[] XList2 =[] YList2 =[] count = 0 boundX = -8 boundY = 10 x1 = - (weightPlot[2]/weightPlot[1]) y1 = 0 x2 = 0 y2 = - (weightPlot[2]/weightPlot[0]) #print x1 , y2 # compute some random point with slope as W and bias b plotTup = getMultiplePoints(x1,y2,weightPlot,boundX,boundY) for row in dataset: if(count<=9): XList1.append(row[0]) YList1.append(row[1]) else: XList2.append(row[0]) YList2.append(row[1]) count = count+1 #Draw points with red and Blue color plt.plot(XList1, YList1, 'ro',XList2, YList2, 'bo') plt.axis([boundX, boundY, boundX, boundY]) plt.plot(plotTup[0],plotTup[1]) plt.show() #Update weight and bias def updateWeight(weights,x,l_rate,error): #update bias weights[2] = weights[2] + x[2] + l_rate * error #update weight part w1, w2 for i in range(len(x)-1): weights[i] = weights[i] + l_rate * error * x[i] return weights #Find linear classifier, predit outcome for each point and if error compute weight def findPerceptronClassifier(dataset,weights): flag = True epoch = 0 retList = [] l_rate = 0.2 count = 0 #lastWeight = [] while(flag): #flag = False epoch = epoch + 1 #print("\nepoch = epoch + 1 is %d\n",epoch) count = 0 for row in dataset: predicted_val = compute(row, weights) error = row[-1] - predicted_val #update weights if error != 0: weights = updateWeight(weights,row,l_rate,error) count = count + 1 lastWeight = weights if error == 0 and count == 0: flag = False else: flag = True retList.append(epoch) #print "Weight is ",weights #print "last Weight is ",lastWeight retList.append(weights) return retList # Input dataset for classifier datasetC1C2 =[[0.1,1.1,0], [6.8 ,7.1,0], [-3.5 ,-4.1,0], [2.0 ,2.7,0] , [4.1 ,2.8,0] , [3.1 ,5.0,0], [-0.8 ,-1.3,0],[0.9 ,1.2,0], [5.0 ,6.4,0], [3.9, 4.0,0], [7.1 ,4.2,1], [-1.4, -4.3,1],[4.5 ,0.0,1 ], [6.3 ,1.6,1 ],[4.2 ,1.9,1 ], [1.4 ,-3.2,1], [2.4 ,-4.0,1 ],[2.5 ,-6.1,1 ],[8.4 ,3.7,1], [4.1 ,-2.2,1]] datasetC2C3 = [[-3.0 , -2.9,0], [0.5, 8.7,0], [2.9 , 2.1,0], [-0.1, 5.2,0], [-4.0 , 2.2,0], [-1.3, 3.7,0], [-3.4, 6.2,0], [-4.1, 3.4,0], [-5.1, 1.6,0], [1.9 , 5.1,0],[7.1 ,4.2,1], [-1.4, -4.3,1],[4.5 ,0.0,1 ], [6.3 ,1.6,1 ],[4.2 ,1.9,1 ], [1.4 ,-3.2,1], [2.4 ,-4.0,1 ],[2.5 ,-6.1,1 ],[8.4 ,3.7,1], [4.1 ,-2.2,1]] #initialize inital weight and bias initial_weights = [0,0,0] #Iteration count epoch = 0 outList = [] def C1C2Classifier(): #Iteration count for convergence - Dataset C1 and C2 outList = findPerceptronClassifier(datasetC1C2,initial_weights) epoch = outList[0] weightPlot = outList[1] ##print "Weight plot is ",weightPlot plotCoordinates(datasetC1C2,weightPlot) def C2C3Classifier(): #Iteration count for convergence - Dataset C2 and C3 outList = findPerceptronClassifier(datasetC2C3,initial_weights) epoch = outList[0] weightPlot = outList[1] ##print "Weight plot is ",weightPlot plotCoordinates(datasetC2C3,weightPlot) # map the inputs to the function blocks options = { 1 : C1C2Classifier, 2 : C2C3Classifier, } #start if __name__ == '__main__': print "1. Run C1C2 classifier \n2. Run C2C3 classifier\n" print "Enter your choice:\t" num = int(raw_input()) options[num]()

Sample output:-

[zytham@s158519-vm perceptron]$ python Perceptron.py

1. Run C1C2 classifier

2. Run C2C3 classifier

Enter your choice: 1

[zytham@s158519-vm perceptron]$ python Perceptron.py

1. Run C1C2 classifier

2. Run C2C3 classifier

Enter your choice:

2

Tags:
MLandAI

I really liked and I got some innovative ideas for improving my thoughts from well defined content.

ReplyDeleteIELTS Coaching in Chennai

IELTS Coaching centre in Chennai

French Classes in Chennai

pearson vue test center in chennai

Informatica Training in Chennai

Data Analytics Courses in Chennai

spanish classes in chennai

content writing course in chennai

IELTS Coaching in OMR

IELTS Coaching in Porur

Very nice blog i really enjoyed to read this blog, continue to post useful information...

ReplyDeleteDevOps Training in Chennai

DevOps Training in Bangalore

Best DevOps Training in Bangalore

DevOps Course in Bangalore

DevOps Training Bangalore

DevOps Training Institutes in Bangalore

DevOps Training in Marathahalli

AWS Training in Bangalore

Data Science Courses in Bangalore

PHP Training in Bangalore

Wow! Such an amazing and helpful post this is. I really really love it. I hope that you continue to do your work like this in the future also.

ReplyDeleteEthical Hacking Training in Bangalore

Ethical Hacking Training

Mua vé tại Aivivu, tham khảo

ReplyDeleteVe may bay di My

bay về việt nam từ mỹ

vé máy bay từ canada về việt nam giá rẻ

có chuyến bay từ nhật về việt nam chưa

vé máy bay từ hàn về Việt Nam

Vé máy bay từ Đài Loan về VN

khách sạn cách ly

chuyen bay chuyen gia trung quoc