# if you want help: help() or ? # ipython: # %load_ext autoreload # %autoreload 2 import numpy as np import matplotlib.pyplot as plt from sklearn import datasets # naive implementation of a Perceptron def perc(X, y, theta=0.5): n, d = X.shape # n samples, d variables a = np.ones(d+1) # a = [1,....,1] Z = np.zeros((n, d+1)) # find a better way: for i in np.arange(n): Z[i, 0] = y[i] Z[i, 1:(d+1)] = y[i] * X[i,:] eta = 0.1 # try with a variable rate k = 0 grad = 2*theta while np.abs(grad) > theta: grad_v = np.zeros(d+1) for i in np.arange(n): if np.dot(a, Z[i,:]) < 0: # i-th sample is misclassified a += eta*Z[i,:] k += 1 grad_v += Z[i,:] print(k) grad = eta * np.linalg.norm(grad_v) # norm return a def perc_clsf(X, a): d = a[0] d += np.dot(X, a[1:]) c = np.ones(X.shape[0], dtype=np.int) c[d < 0] = -1 return c def plot_clsf_reg(X, y, a): xmn, xmx = X[:,0].min() - 1, X[:,0].max() + 1 ymn, ymx = X[:,1].min() - 1, X[:,1].max() + 1 xx, yy = np.meshgrid(np.arange(xmn,xmx,0.02), np.arange(ymn,ymx,0.02)) Z = perc_clsf(np.c_[xx.ravel(),yy.ravel()], a) # for plotting, convert to 0, 1: Z = (Z + 1) / 2 Z = Z.reshape(xx.shape) plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8) # add the points with plt.scatter(X[:,0], X[:,1], c=y, cmap=plt.cm.Paired) plt.xlim(xx.min(), xx.max()) plt.ylim(yy.min(), yy.max()) plt.show() # Use: iris = datasets.load_iris() X = iris.data[:, :2] # we only take the first two features. We could # avoid this ugly slicing by using a two-dim dataset y = iris.target # Just 2 classes... y[y > 0] = -1 y[y == 0] = 1 a = perc(X, y) yy = perc_clsf(X, a) # compare y to yy (error rate): np.sum(y != yy) / yy.size # try a = perc(X, y, 0.1) a = perc(X, y, 0.05) plot_clsf_reg (X, y, a)