The Smallest Brain You Can Build: A Perceptron in Python

246 points · 45 comments on HN · read original →

Points and comments are a snapshot, not live.

A tutorial building a perceptron from scratch in Python, explaining how it learns binary classification through weight and bias adjustments.

A perceptron is a single-neuron classifier that takes one input, multiplies it by a weight, adds a bias, and outputs yes or no if the sum exceeds zero. The author explains the mechanism through two examples: classifying positive versus negative numbers, then determining student pass-fail from exam scores. The bias moves the decision boundary left or right; without it, the boundary is stuck at zero and cannot solve problems where the answer lies elsewhere. Learning occurs iteratively through epochs: when a prediction is wrong, the weight and bias are nudged proportionally to the error and input value. The author demonstrates how data normalization smooths training convergence, especially when inputs have different scales. The complete working code is shown. The post concludes that stacking perceptrons into layers creates neural networks capable of learning nonlinear patterns, but each neuron performs the same basic operation.

What commenters are saying

Top-ranked commenters recommend foundational textbooks and courses (Bishop's Deep Learning book, fast.ai) over ad hoc demos for learning ML fundamentals. One commenter notes Bishop released a new collaborative textbook with his son and shares it is freely available online. A separate thread celebrates the minimalist approach: one developer linked a JavaScript implementation called NanoNeuron; another joked that f(x)=0 is a smaller brain. Several comments pivot to hardware and history, tracing the perceptron to RTL circuits, transistors, and computational primitives like logic gates, exploring how these concepts map to physical implementations. No major corrections or disputes surface; sentiment is broadly positive toward the pedagogical clarity.