Run in Python3


In [3]:
import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline

fig = plt.figure()

X = np.array([
    [0.2, 0.1],
    [0.4, 0.6],
    [0.5, 0.2],
    [0.7, 0.9]
])

y = [0, 0, 0, 1]

markers = ['.', 'x']
plt.scatter(X[:3, 0], X[:3, 1], marker='.', s=400)
plt.scatter(X[3, 0], X[3, 1], marker='x', s=400)
plt.xlabel('Proportion of the day spent sleeping')
plt.ylabel('Proportion of the day spent being grumpy')
plt.title('Kittens and Adult Cats')
# plt.plot([0, -2.72], [-3.3, 0])
# plt.plot([0, -13.33], [-3.6363, 0])
#plt.plot([0, -30], [4.286, 0])
#plt.plot([0, 1], [0, 1])
plt.show()

#fig.savefig('graph.png')

######################

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

X = np.array([
    [0.2, 0.1],
    [0.4, 0.6],
    [0.5, 0.2],
    [0.7, 0.9]
])
X_test = np.array([
    [0.7, 0.8]
])

Y = np.array([1, 1, 1, 0])
h = 0.001 # h = 0.02

# create a mesh to plot in
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                     np.arange(y_min, y_max, h))
fig = plt.figure(figsize=(27, 9))

for e in range(1, 7):
    print ('\nStarting epoch', e)
    ax = plt.subplot(2, 5, e)

    clf = Perceptron(n_iter=e, verbose=5).fit(X, Y)
    print (clf.intercept_, clf.coef_)
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)

    ax.contourf(xx, yy, Z, cmap=plt.cm.Paired)

    # Plot also the training points
    ax.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired)

    plt.title('Epoch %s' % e)
    plt.xlabel('Proportion of the day spent sleeping')
    plt.ylabel('Proportion of the day spent being grumpy')
    plt.title('Kittens and Adult Cats')

    if clf.score(X, Y) == 1:
        print ('converged in epoch', e)
        break

fig.subplots_adjust(left=.02, right=.98)

plt.show()
#fig.savefig('graph1.png')


Starting epoch 1
-- Epoch 1
Norm: 0.42, NNZs: 2, Bias: 0.000000, T: 4, Avg. loss: 0.455000
Total training time: 0.00 seconds.
[ 0.] [[-0.3 -0.3]]

Starting epoch 2
-- Epoch 1
Norm: 0.42, NNZs: 2, Bias: 0.000000, T: 4, Avg. loss: 0.455000
Total training time: 0.00 seconds.
-- Epoch 2
Norm: 1.12, NNZs: 2, Bias: 0.000000, T: 8, Avg. loss: 0.385000
Total training time: 0.00 seconds.
[ 0.] [[-0.5 -1. ]]

Starting epoch 3
-- Epoch 1
Norm: 0.42, NNZs: 2, Bias: 0.000000, T: 4, Avg. loss: 0.455000
Total training time: 0.00 seconds.
-- Epoch 2
Norm: 1.12, NNZs: 2, Bias: 0.000000, T: 8, Avg. loss: 0.385000
Total training time: 0.00 seconds.
-- Epoch 3
Norm: 0.95, NNZs: 2, Bias: 1.000000, T: 12, Avg. loss: 0.273333
Total training time: 0.00 seconds.
[ 1.] [[-0.3 -0.9]]
converged in epoch 3

In [ ]:


In [ ]: