#179Perceptron — the learning ruleMedium

Perceptron — the learning rule

Background

The perceptron learns by fixing its mistakes. Starting from zero weights, it sweeps the data for epochs passes; on each example it predicts y^=1[wx+b0]\hat{y} = \mathbb{1}[\mathbf{w}^\top\mathbf{x}+b \ge 0] and nudges the parameters toward the truth:

ww+η(yy^)x,bb+η(yy^)\mathbf{w} \leftarrow \mathbf{w} + \eta\,(y - \hat{y})\,\mathbf{x}, \qquad b \leftarrow b + \eta\,(y - \hat{y})

If the prediction is right, yy^=0y - \hat{y} = 0 and nothing changes. If wrong, the weights move toward the misclassified point. For linearly separable data this is guaranteed to converge to a perfect separator.

Problem statement

Implement train_perceptron(X, y, epochs, lr) that runs the rule above from a zero start and returns the learned (w, b).

Input

  • X — array-like of shape (m, n): one example per row.
  • y — array-like of m labels (0/1).
  • epochsint: number of full passes over the data.
  • lrfloat: the learning rate η\eta.

Output

Returns a tuple (w, b) where w is an np.ndarray of shape (n,) and b is a float.

Examples

Example — the AND gate becomes separable

Input:  X = [[0,0],[0,1],[1,0],[1,1]], y = [0,0,0,1], epochs = 20, lr = 1.0
Output: (w, b) that classify all four points correctly:
        perceptron([0,0], w, b)=0, [0,1]=0, [1,0]=0, [1,1]=1

Explanation: AND is linearly separable, so the rule converges to a valid boundary.

Constraints

  • Initialise w = zeros(n) and b = 0.0.
  • Process examples in order within each epoch, updating after each one.
  • Use the step prediction 1[z0]\mathbb{1}[z \ge 0] inside the loop.
  • Return w as shape (n,) and b as a float.

Notes

  • A single perceptron can only separate linearly separable classes — it will never fit XOR, no matter how many epochs. That limitation is what motivates hidden layers and depth.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Example — learns the AND gate
  • Reference learns the OR gate
  • Sample zero learning rate stays at zero init