1. Home
  2. AI & Machine Learning
  3. Perceptron

Perceptron

The simplest neural network: a single neuron that learns a straight line to separate two classes by fixing its mistakes one at a time.

Interactive 3DBeginner12 min readAI/MLUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · ←→ step

What's happening

Pseudocode

    Try this in the 3D model

    • Press Train and count how many updates it takes. Does the line stop moving once all points are right?
    • Change the learning rate to 0.1 and train on the same data. Does it still converge, and how many updates?
    • Press New data a few times. Does training always finish? Why?
    • Watch the white arrow w. Which way does it point relative to the +1 points?

    One neuron, one line

    A perceptron takes some numbers (features), multiplies each by a weight, adds a bias and checks the sign:

    ŷ = +1  if  w₁x₁ + w₂x₂ + b ≥ 0
        −1  otherwise

    Geometrically, w·x + b = 0 is a straight decision line. Points on one side are class +1 and the rest class −1. The weight vector w is perpendicular to the line and points towards the +1 side.

    Learning from mistakes

    Start with arbitrary weights and sweep through the training points. Whenever a point is classified wrongly, nudge the line towards it:

    for each point (x, y):
        if sign(w·x + b) ≠ y:
            w ← w + η · y · x
            b ← b + η · y

    η is the learning rate. For a +1 point that was predicted −1 the update adds the point to w, which raises its score; a −1 point is subtracted. Repeat for several epochs until a full pass makes no mistakes.

    Does it always work?

    If the classes can be separated by a straight line (they are linearly separable), the perceptron convergence theorem says the algorithm stops after a finite number of updates. If they cannot be separated, it keeps wandering forever. The classic failure is XOR: no single line separates (0,0), (1,1) from (0,1), (1,0). That limitation motivated multi-layer networks (see neural networks).

    Model Output Learns by
    Perceptron Hard class (+1 or −1) fixing mistakes
    Logistic regression Probability gradient descent on log loss
    SVM Hard class with the widest margin maximising the margin

    Code

    def train_perceptron(points, labels, lr=1.0, epochs=30):
        w, b = [0.0, 0.0], 0.0
        for _ in range(epochs):
            mistakes = 0
            for (x1, x2), y in zip(points, labels):
                pred = 1 if w[0] * x1 + w[1] * x2 + b >= 0 else -1
                if pred != y:
                    w[0] += lr * y * x1
                    w[1] += lr * y * x2
                    b += lr * y
                    mistakes += 1
            if mistakes == 0:
                break
        return w, b

    Common mistakes

    • Updating on every point instead of only on mistakes.
    • Forgetting the bias b. Without it the line must pass through the origin.
    • Expecting convergence on data that is not linearly separable.
    • Using labels 0/1 with the formula w + η·y·x. This version needs labels +1/−1.

    Complexity at a glance

    Case / operationTimeWhy
    One predictionO(d)A dot product over d features.
    One epochO(n · d)n points, d features each.
    Mistakes before convergence≤ (R/γ)²Novikoff's bound for separable data with margin γ and radius R.
    Extra spaceO(d) weights

    Quick check

    Test yourself — pick an answer to see if you got it.

    1. What does a perceptron compute?

    2. When are the weights updated?

    3. What is the update for a misclassified point (x, y)?

    4. Which dataset can a single perceptron NOT learn?

    Saved only in this browser — no account needed.
    Spotted a mistake or a bug in the 3D model?

    Report a mistake

    in Perceptron. Thank you — every report makes the lesson better for the next reader.

    We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.