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')
In [ ]:
In [ ]: