Skip to content

Instantly share code, notes, and snippets.

@Niranjankumar-c
Created February 21, 2019 13:52
Show Gist options
  • Save Niranjankumar-c/a397e13d8f19e61e70ae91f77b05dce0 to your computer and use it in GitHub Desktop.
Save Niranjankumar-c/a397e13d8f19e61e70ae91f77b05dce0 to your computer and use it in GitHub Desktop.
Building Perceptron Model
class Perceptron:
#constructor
def __init__ (self):
self.w = None
self.b = None
#model
def model(self, x):
return 1 if (np.dot(self.w, x) >= self.b) else 0
#predictor to predict on the data based on w
def predict(self, X):
Y = []
for x in X:
result = self.model(x)
Y.append(result)
return np.array(Y)
def fit(self, X, Y, epochs = 1, lr = 1):
self.w = np.ones(X.shape[1])
self.b = 0
accuracy = {}
max_accuracy = 0
wt_matrix = []
#for all epochs
for i in range(epochs):
for x, y in zip(X, Y):
y_pred = self.model(x)
if y == 1 and y_pred == 0:
self.w = self.w + lr * x
self.b = self.b - lr * 1
elif y == 0 and y_pred == 1:
self.w = self.w - lr * x
self.b = self.b + lr * 1
wt_matrix.append(self.w)
accuracy[i] = accuracy_score(self.predict(X), Y)
if (accuracy[i] > max_accuracy):
max_accuracy = accuracy[i]
chkptw = self.w
chkptb = self.b
#checkpoint (Save the weights and b value)
self.w = chkptw
self.b = chkptb
print(max_accuracy)
#plot the accuracy values over epochs
plt.plot(accuracy.values())
plt.ylim([0, 1])
plt.show()
#return the weight matrix, that contains weights over all epochs
return np.array(wt_matrix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment