Skip to content

Instantly share code, notes, and snippets.

@TetsuyaYoshimoto
Last active December 16, 2016 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TetsuyaYoshimoto/149b225d8a15d4a757eeff874f69c55b to your computer and use it in GitHub Desktop.
Save TetsuyaYoshimoto/149b225d8a15d4a757eeff874f69c55b to your computer and use it in GitHub Desktop.
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