May 1, 2013

Scikit Learn - Simple example of a Perceptron

The following is a quick example of scikit-learn's Perceptron.

Two arrays x and y form the training set. In this case we have 5 training points and their corresponding target outputs (target classes). After fitting the perceptron to this training set, the perceptron is tested using a test set containing points starting from (-2,-2) to (8,8).

In the predicted output (plotted below), we can see that the perceptron was able to learn the boundary between the two classes that we trained it for.

import numpy as np
from sklearn.linear_model import Perceptron
import matplotlib.pyplot as plt

x = np.array([ [3,1],
               [3,2],
               [4,1],
               [2,3],
               [1,4] ])

y = np.array([1,
              1,
              1,
              -1,
              -1])

ptraina = plt.scatter(x[0:3,0], x[0:3,1], marker='o', color='Turquoise', s=150)
ptrainb = plt.scatter(x[3:,0], x[3:,1], marker='o', color='Plum',  s=150)

p = Perceptron()
p.fit(x,y)

a,b = np.mgrid[-2:8:1, -2:8:1]
c = np.dstack((a,b))
xtest = c.reshape(np.shape(c)[0]*np.shape(c)[1],2)
res = p.predict(xtest)

ptesta = ptestb = None

for i, item in enumerate(res):
    if item == -1:
        ptestb = plt.scatter(xtest[i][0], xtest[i][1], marker='o', color='r')
    else:
        ptesta = plt.scatter(xtest[i][0], xtest[i][1], marker='o', color='b')

plt.legend([ptraina, ptrainb, ptesta, ptestb], ["Train: Class-A", "Train: Class-B", "Test: Class-A", "Test: Class-B"])

plt.show()
Sample Output


We can see that the perceptron's weights after training are the following. This gives a hypothesis function y=3x1-4x2, which when plotted below shows the boundary arrived at by the perceptron.
In [56]: p.coef_
Out[56]: array([[ 3., -4.]])

In [57]: p.intercept_
Out[57]: array([ 0.])

Plotting the decision boundary
In [68]: X=np.arange(-2,8,1)

In [69]: Y=(3./4)*X

In [70]: plt.plot(X,Y)