SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Perceptron(object):
  2.     """Perceptron classifier.
  3.  
  4.     Parameters
  5.     ------------
  6.     eta : float
  7.       Learning rate (between 0.0 and 1.0)
  8.     n_iter : int
  9.       Passes over the training dataset.
  10.     random_state : int
  11.       Random number generator seed for random weight
  12.       initialization.
  13.  
  14.     Attributes
  15.     -----------
  16.     w_ : 1d-array
  17.       Weights after fitting.
  18.     errors_ : list
  19.       Number of misclassifications (updates) in each epoch.
  20.  
  21.     """
  22.     def __init__(self, eta=0.01, n_iter=50, random_state=1):
  23.         self.eta = eta
  24.         self.n_iter = n_iter
  25.         self.random_state = random_state
  26.  
  27.     def fit(self, X, y):
  28.         """Fit training data.
  29.  
  30.         Parameters
  31.         ----------
  32.         X : {array-like}, shape = [n_samples, n_features]
  33.           Training vectors, where n_samples is the number of samples and
  34.           n_features is the number of features.
  35.         y : array-like, shape = [n_samples]
  36.           Target values.
  37.  
  38.         Returns
  39.         -------
  40.         self : object
  41.  
  42.         """
  43.         rgen = np.random.RandomState(self.random_state)
  44.         self.w_ = rgen.normal(loc=0.0, scale=0.01, size=1 + X.shape[1]) #Draw random samples from a normal (Gaussian) distribution.
  45.         self.errors_ = []
  46.  
  47.         for _ in range(self.n_iter):
  48.             errors = 0
  49.             for xi, target in zip(X, y):
  50.                 update = self.eta * (target - self.predict(xi))
  51.                 self.w_[1:] += update * xi
  52.                 self.w_[0] += update
  53.                 errors += int(update != 0.0)
  54.             self.errors_.append(errors)
  55.         return self
  56.  
  57.     def net_input(self, X):
  58.         """Calculate net input"""
  59.         return np.dot(X, self.w_[1:]) + self.w_[0]
  60.  
  61.     def predict(self, X):
  62.         """Return class label after unit step"""
  63.         return np.where(self.net_input(X) >= 0.0, 1, -1)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top