Created
February 21, 2019 13:53
-
-
Save Niranjankumar-c/ac373c9ed44251a44f07b0445c37b483 to your computer and use it in GitHub Desktop.
Building Perceptron Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.xlabel("Epoch #") | |
plt.ylabel("Accuracy") | |
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