1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
from __future__ import print_function from __future__ import division import numpy as np import cPickle as pkl from generate_data import generate_data, plot_data __author__ = 'wangzx'
class Perceptron(object): """ A Perceptron instance can take a function and attempt to ``learn`` a bias and set of weights that compute that function, using the perceptron learning algorithm."""
def __init__(self, inputs): """ Initialize the perceptron with the bias and all weights set to 0.0. ``inputs`` is the input to the perceptron.""" num_inputs = inputs.shape[1] self.num_inputs = num_inputs self.bias = 0.0 self.weights = np.zeros(num_inputs) self.inputs = inputs
def output(self, x): """ Return the output (0 or 1) from the perceptron, with input ``x``.""" return 1 if np.inner(self.weights, x)+self.bias > 0 else -1
def learn(self, y, eta=0.1, max_epoch=100): self.bias = np.random.normal() self.weights = np.random.randn(self.num_inputs) number_of_errors = -1 epoch = 0 while number_of_errors != 0 and epoch < max_epoch: number_of_errors = 0 epoch += 1 for i, x in enumerate(self.inputs): y_pre = self.output(x) if y[i] != y_pre: number_of_errors += 1 self.bias = self.bias + eta*y[i] self.weights = self.weights + eta*y[i]*x def predict(self, X): res = [self.output(x) for x in X] return np.asarray(res) def test(): feat = pkl.load(open("data/train_X.pkl", 'rb')) y = pkl.load(open("data/train_Y.pkl", 'rb')) y[y==0] = -1 test_feat = pkl.load(open("data/test_X.pkl", 'rb')) test_y = pkl.load(open("data/test_Y.pkl", 'rb')) test_y[test_y==0] = -1 pla = Perceptron(feat)
print("Begin fit training data") pla.learn(y, eta=1)
y_prd = pla.predict(test_feat) score = np.sum(y_prd == test_y) / y_prd.shape[0] print("Test accuracy is %f" % score)
def test_gdata(): mean = [[-2,7], [1,1]] cov1 = [[1,0.5], [0.5, 3]] cov2 = [[1,0], [0,1]] g_data = generate_data(mean, [cov1, cov2]) X = [f for f, t in g_data] X = np.asarray(X) y = [t for f, t in g_data] y = np.asarray(y) y[y==0] = -1
pla = Perceptron(X) print("Begin fit training data") pla.learn(y, eta=1, max_epoch=300)
y_prd = pla.predict(X) score = np.sum(y_prd == y) / y_prd.shape[0] print("Test accuracy is %f" % score) border_line(pla, g_data)
def border_line(pla, g_data): b = pla.bias w = pla.weights x = np.asarray([[-8, 6]]) y = -(w[0]*x + b) / w[1] plot_data(g_data, np.concatenate((x, y)).T) if __name__ == "__main__": pass
|