Last active
December 11, 2021 20:23
-
-
Save loiseaujc/51d9985d2f6e6dec4887ba8052aaab94 to your computer and use it in GitHub Desktop.
Implementation of Rosenblatt's perceptron using Python.
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
# --> Import standard Python libraries. | |
import numpy as np | |
# --> Import sklearn utility functions to create derived-class objects. | |
from sklearn.base import BaseEstimator, ClassifierMixin | |
# --> Redefine the Heavisde function. | |
H = lambda x: np.heaviside(x, 1).astype(np.int) | |
class Rosenblatt(BaseEstimator, ClassifierMixin): | |
""" | |
Implementation of Rosenblatt's Perceptron using sklearn BaseEstimator and | |
ClassifierMixin. | |
""" | |
def __init__(self): | |
return | |
def predict(self, X): | |
return H( X.dot(self.weights) + self.bias ) | |
def fit(self, X, y, epochs=100): | |
""" | |
Implementation of the Perceptron Learning Algorithm. | |
INPUT | |
----- | |
X : numpy 2D array. Each row corresponds to one training example. | |
y : numpy 1D array. Label (0 or 1) of each example. | |
OUTPUT | |
------ | |
self : The trained perceptron model. | |
""" | |
# --> Number of features. | |
n = X.shape[1] | |
# --> Initialize the weights and bias. | |
self.weights = np.zeros((n, )) | |
self.bias = 0.0 | |
# --> Perceptron algorithm loop. | |
for _ in range(epochs): | |
# --> Current number of errors. | |
errors = 0 | |
# --> Loop through the examples. | |
for xi, y_true in zip(X, y): | |
# --> Compute error. | |
error = y_true - self.predict(xi) | |
if error != 0: | |
# --> Update the weights and bias. | |
self.weights += error * xi | |
self.bias += error | |
# --> Current number of errors. | |
errors += 1 | |
# --> If no error is made, exit the outer for loop. | |
if errors == 0: | |
break | |
return self |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment