Last active
December 16, 2016 08:58
-
-
Save TetsuyaYoshimoto/149b225d8a15d4a757eeff874f69c55b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Perceptron(object): | |
def __init__(self, eta=0.01, n_iter=10): | |
self.eta = eta | |
self.n_iter = n_iter | |
def fit(self, X, y): | |
self.w_ = np.zeros(1 + X.shape[1]) | |
self.errors_ = [] | |
for _ in range(self.n_iter): | |
errors = 0 | |
for xi, target in zip(X, y): | |
update = self.eta * (target - self.predict(xi)) | |
self.w_[1:] += update * xi | |
self.w_[0] += update | |
errors += np.where(update == 0.0, 0, 1) | |
self.errors_.append(errors) | |
return self | |
def net_input(self, X): | |
return np.dot(X, self.w_[1:]) + self.w_[0] | |
def predict(self, X): | |
return np.where(self.net_input(X) >= 0.0, 1, -1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment