| 29 | |
| 30 | class Perceptron: |
| 31 | def fit(self, X, Y, learning_rate=1.0, epochs=1000): |
| 32 | # solution |
| 33 | # self.w = np.array([-0.5, 0.5]) |
| 34 | # self.b = 0.1 |
| 35 | |
| 36 | # initialize random weights |
| 37 | D = X.shape[1] |
| 38 | self.w = np.random.randn(D) |
| 39 | self.b = 0 |
| 40 | |
| 41 | N = len(Y) |
| 42 | costs = [] |
| 43 | for epoch in range(epochs): |
| 44 | # determine which samples are misclassified, if any |
| 45 | Yhat = self.predict(X) |
| 46 | incorrect = np.nonzero(Y != Yhat)[0] |
| 47 | if len(incorrect) == 0: |
| 48 | # we are done! |
| 49 | break |
| 50 | |
| 51 | # choose a random incorrect sample |
| 52 | i = np.random.choice(incorrect) |
| 53 | self.w += learning_rate*Y[i]*X[i] |
| 54 | self.b += learning_rate*Y[i] |
| 55 | |
| 56 | # cost is incorrect rate |
| 57 | c = len(incorrect) / float(N) |
| 58 | costs.append(c) |
| 59 | print("final w:", self.w, "final b:", self.b, "epochs:", (epoch+1), "/", epochs) |
| 60 | plt.plot(costs) |
| 61 | plt.show() |
| 62 | |
| 63 | def predict(self, X): |
| 64 | return np.sign(X.dot(self.w) + self.b) |