One of the best ways to understand the perceptron is to build it yourself. A library call is useful for real work, but a scratch implementation shows exactly how prediction, weight updates, and training loops fit together.
This article walks through a simple single-layer perceptron in Python. The goal is not to build the fastest implementation. The goal is to make every line of the learning logic easy to understand.
What you will learn
- how to implement the perceptron prediction step
- how the weight update rule works in code
- how to organize the training loop
- what to inspect when the model is not learning as expected
Why build it from scratch
When you implement the perceptron yourself, you see the model as a sequence of simple operations:
- compute a weighted sum
- apply a threshold-style rule
- compare prediction and target
- update the parameters when needed
That understanding makes later topics such as logistic regression, gradient-based learning, and multilayer neural networks much easier to follow.
The core class
import numpy as np
class Perceptron:
def __init__(self, learning_rate=0.1, epochs=20):
self.learning_rate = learning_rate
self.epochs = epochs
self.weights = None
self.bias = 0.0
def predict(self, X):
linear_output = np.dot(X, self.weights) + self.bias
return np.where(linear_output >= 0.0, 1, 0)
def fit(self, X, y):
n_features = X.shape[1]
self.weights = np.zeros(n_features, dtype=float)
for _ in range(self.epochs):
for features, target in zip(X, y):
prediction = self.predict(features)
update = self.learning_rate * (target - prediction)
self.weights += update * features
self.bias += update
return self
How it works
The class has only a few moving parts:
weightsstore how strongly each feature affects the decisionbiasshifts the decision boundarypredict()computes the linear score and threshold outputfit()loops over the training examples and updates parameters after mistakes
The update rule is the most important line:
update = learning_rate * (target - prediction)
If the prediction matches the target, the update is zero. If the prediction is wrong, the weights move in the direction that makes the correct class more likely next time.
A tiny usage example
X = np.array([
[2.0, 1.0],
[1.0, 1.0],
[-1.0, -1.0],
[-2.0, -1.0],
])
y = np.array([1, 1, 0, 0])
model = Perceptron(learning_rate=0.1, epochs=10)
model.fit(X, y)
predictions = model.predict(X)
print(predictions)
If the data is linearly separable, the perceptron should find a useful boundary with repeated passes over the training set.
What this example teaches
This small implementation is enough to show the full logic of the algorithm. You do not need a large framework to understand the perceptron. You just need to understand the relation between score, threshold, error, and update.
If you want a more complete dataset example after this, read Perceptron on the iris dataset in python.
Common mistakes or limitations
- using labels in an inconsistent format
- expecting convergence on non-linearly separable data
- forgetting to inspect feature scale
- assuming the perceptron outputs probabilities
If the data pattern is fundamentally non-linear, no amount of training will make a single perceptron solve it perfectly. That is why XOR remains the classic warning example.
Key takeaways
- A scratch implementation makes the perceptron much easier to understand.
- The core steps are weighted sum, threshold prediction, and mistake-driven updates.
- The algorithm is simple, but it only works well on linearly separable problems.
Next steps
- Read Perceptron explained for beginners.
- Read Perceptron vs logistic regression.
- Read Neural networks basics for the next conceptual step.
References
- Scikit-learn Perceptron documentation: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Perceptron.html