In [13]:
# Import 
%matplotlib inline
from sklearn import datasets
import matplotlib.pyplot as plt
import numpy as np

In [22]:
# import some data to play with
iris = datasets.load_iris()
X = iris.data[:100,:2]  # we only take the first two features.
Y = iris.target[:100]

In [47]:
# Line separating the features. Change this to get lines separating the features
m = 1
c = -2.3

# create line
x1 = np.linspace(np.min(X[:,0]), np.max(X[:,0]), 100)
y1 = m*x1 + c

plt.figure(2, figsize=(8, 6))
plt.clf()

# Plot the training points
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired)
plt.plot(x1,y1)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')


Out[47]:
<matplotlib.text.Text at 0x7f20dc039358>

In [48]:
# Create perceptron
threshold = 1

# Get weights using the line equations
w2 = threshold / c
w1 = -1 * w2 * m

# Perceptron calculation
plant_class = ((X[:,0] * w1 + X[:,1] * w2) > threshold) + 0

# Compare
correctly_classified = np.all(plant_class == Y)

In [49]:
correctly_classified


Out[49]:
True