Subroutine Logo
Subroutine
← Back to Playground AI & ML Beginner

Visualizing Perceptrons & Decision Boundaries

Understand the fundamental building block of artificial neural networks through interactive 2D decision boundary classification.

Published: 2026-07-26
#AI & ML#Neural Networks#Perceptron#Classification

Artificial Intelligence and modern Deep Learning rest upon a humble atomic building block: the Perceptron. Invented by Frank Rosenblatt in 1958, a Single-Layer Perceptron learns a linear hyperplane that separates two distinct classes of data.


1. Summary & Key Takeaways

  • The Linear Equation: A Perceptron calculates a weighted sum of its inputs plus a bias term: z=w1x1+w2x2+bz = w_1 x_1 + w_2 x_2 + b
  • Step Activation Function: If z0z \ge 0, the output is +1+1 (Cyan Class); otherwise, the output is 1-1 (Pink Class).
  • Linear Separability: Single-layer perceptrons can only classify datasets that are linearly separable (meaning a single straight line can divide the classes).
  • Perceptron Learning Rule: Weight updates occur only when a misclassification occurs: wiwi+η(yy^)xiw_i \leftarrow w_i + \eta \cdot (y - \hat{y}) \cdot x_i bb+η(yy^)b \leftarrow b + \eta \cdot (y - \hat{y})

2. Interactive Perceptron Playground

Use the interactive 2D plane below to add data points (Class A vs Class B), adjust weights manually with the sliders, or click “Train 1 Epoch” to watch the perceptron learn the optimal decision boundary line!

Interactive Experiment

Click anywhere on the 2D black canvas grid to drop new custom data points. Watch how training adjusts the decision line’s slope and position!

Single-Layer PerceptronBinary Classification

Click on the 2D plane to add training points. Train the decision boundary w₁x₁ + w₂x₂ + b = 0 in real-time.

Epoch: 0
Accuracy: 63%
Click plane to add point (Cyan Class)
Add Point Type:
Weight w₁:0.60
Weight w₂:-0.80
Bias b:0.10
Perceptron Implementation Code
perceptron.py
Python
import numpy as np

class Perceptron:
    def __init__(self, num_inputs, learning_rate=0.1):
        self.weights = np.zeros(num_inputs)
        self.bias = 0.0
        self.lr = learning_rate

    def predict(self, inputs):
        summation = np.dot(inputs, self.weights) + self.bias
        return 1 if summation >= 0 else -1

    def train(self, inputs, target):
        prediction = self.predict(inputs)
        error = target - prediction
        if error != 0:
            self.weights += self.lr * error * inputs
            self.bias += self.lr * error

3. Mathematical Foundations

The Perceptron computes a linear decision boundary separating two classes in 2D space:

w1x1+w2x2+b=0    x2=w1w2x1bw2w_1 x_1 + w_2 x_2 + b = 0 \implies x_2 = -\frac{w_1}{w_2} x_1 - \frac{b}{w_2}

Where:

  • w1w_1 dictates the slope along the x1x_1 axis.
  • w2w_2 dictates the slope along the x2x_2 axis.
  • bb (bias) shifts the boundary line away from the origin (0,0)(0,0).
  • η\eta (learning rate) controls the magnitude of weight updates per epoch step.

If a dataset is not linearly separable (for example, the XOR problem), a single-layer perceptron will fail to reach 100%100\% accuracy—which led to the development of Multi-Layer Perceptrons (MLPs) and Backpropagation!