Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import generatory_danych as gen
- import matplotlib.pyplot as plt
- class Perceptron:
- def __init__(self, dim, ):
- self.wagi = np.random.sample(dim)
- self.bias = np.random.random()
- def sumator(self, X):
- return np.dot(self.wagi, X) + self.bias
- def fa(self, X):
- if self.sumator(X) > 0:
- return 1
- else:
- return -1
- def predict(self, data):
- Y_pred = np.zeros(len(data))
- for idx, x in enumerate(data):
- Y_pred[idx] = self.fa(x)
- return Y_pred
- def back(self, X, Y):
- if (self.fa(X) != Y):
- self.wagi += Y*X
- self.bias += Y
- def fit(self, data, target, epochs):
- for e in range(epochs):
- for x, y in zip(data, target):
- self.back(x, y)
- print("Epoka {0}: {1}".format(e, self.accuracy(data, target)))
- def accuracy(self, data, target):
- Y_pred = self.predict(data)
- return np.sum(np.abs(Y_pred-target))/2
- ile = 100
- zakres = [[-5,5],[-10,10]]
- wek=[2,1,0]
- dane, klasy = gen.gen_lin_sep(ile, zakres, wek)
- pr1 = Perceptron(2)
- pr1.accuracy(dane, klasy)
- pr1.fit(dane, klasy, epochs=10)
- plt.scatter(dane[:, 0], dane[:, 1], c=klasy)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement