Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. import numpy as np
  2. import generatory_danych as gen
  3. import matplotlib.pyplot as plt
  4.  
  5. class Perceptron:
  6. def __init__(self, dim, ):
  7. self.wagi = np.random.sample(dim)
  8. self.bias = np.random.random()
  9.  
  10. def sumator(self, X):
  11. return np.dot(self.wagi, X) + self.bias
  12.  
  13. def fa(self, X):
  14. if self.sumator(X) > 0:
  15. return 1
  16. else:
  17. return -1
  18.  
  19. def predict(self, data):
  20. Y_pred = np.zeros(len(data))
  21. for idx, x in enumerate(data):
  22. Y_pred[idx] = self.fa(x)
  23. return Y_pred
  24.  
  25. def back(self, X, Y):
  26. if (self.fa(X) != Y):
  27. self.wagi += Y*X
  28. self.bias += Y
  29.  
  30. def fit(self, data, target, epochs):
  31. for e in range(epochs):
  32. for x, y in zip(data, target):
  33. self.back(x, y)
  34. print("Epoka {0}: {1}".format(e, self.accuracy(data, target)))
  35.  
  36. def accuracy(self, data, target):
  37. Y_pred = self.predict(data)
  38. return np.sum(np.abs(Y_pred-target))/2
  39.  
  40. ile = 100
  41. zakres = [[-5,5],[-10,10]]
  42. wek=[2,1,0]
  43.  
  44. dane, klasy = gen.gen_lin_sep(ile, zakres, wek)
  45.  
  46.  
  47. pr1 = Perceptron(2)
  48. pr1.accuracy(dane, klasy)
  49. pr1.fit(dane, klasy, epochs=10)
  50.  
  51. plt.scatter(dane[:, 0], dane[:, 1], c=klasy)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement