Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. import numpy as np
  2.  
  3. class Perceptron(object):
  4. """ Perceptron Classifier
  5.  
  6. Parameters
  7. ------------
  8. rate : float
  9. Learning rate (ranging from 0.0 to 1.0)
  10. number_of_iteration : int
  11. Number of iterations over the input dataset.
  12.  
  13. Attributes:
  14. ------------
  15.  
  16. weight_matrix : 1d-array
  17. Weights after fitting.
  18.  
  19. error_matrix : list
  20. Number of misclassification in every epoch(one full training cycle on the training set)
  21.  
  22. """
  23.  
  24. def __init__(self, rate = 0.01, number_of_iterations = 100):
  25. self.rate = rate
  26. self.number_of_iterations = number_of_iterations
  27.  
  28. def fit(self, X, y):
  29. """ Fit training data
  30.  
  31. Parameters:
  32. ------------
  33. X : array-like, shape = [number_of_samples, number_of_features]
  34. Training vectors.
  35. y : array-like, shape = [number_of_samples]
  36. Target values.
  37.  
  38. Returns
  39. ------------
  40. self : object
  41.  
  42. """
  43.  
  44. self.weight_matrix = np.zeros(1 + X.shape[1])
  45. self.errors_list = []
  46.  
  47. for _ in range(self.number_of_iterations):
  48. errors = 0
  49. for xi, target in zip(X, y):
  50. update = self.rate * (target - self.predict(xi))
  51. self.weight_matrix[1:] += update * xi
  52. self.weight_matrix[0] += update
  53. errors += int(update != 0.0)
  54. self.errors_list.append(errors)
  55. return self
  56.  
  57. def dot_product(self, X):
  58. """ Calculate the dot product """
  59. return (np.dot(X, self.weight_matrix[1:]) + self.weight_matrix[0])
  60.  
  61. def predict(self, X):
  62. """ Predicting the label for the input data """
  63. return np.where(self.dot_product(X) >= 0.0, 1, 0)
  64.  
  65.  
  66. if __name__ == '__main__':
  67. X = np.array([[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0]])
  68. y = np.array([0, 1, 1, 1, 1, 1, 1])
  69. p = Perceptron()
  70. p.fit(X, y)
  71. print("Predicting the output of [1, 1, 1] = {}".format(p.predict([1, 1, 1])))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement