Can you predict whether a sentence sounds happy or sad from the words it contains? A perceptron is a classifier that gives each input feature a weight, adds a bias, and uses the resulting score to choose between two categories. Unlike regression, which predicts a number, it predicts a category. section 5.1 section 5.6 section 5.0
A first sentiment classifier
Count two words in a sentence: give each appearance of “aack” one point and each “beep” minus one point. A score of zero or more predicts happy; a negative score predicts sad. Plotting the two counts reveals the rule as a diagonal line: on the line, the counts are equal. section 5.1
Figure 5.3 The classifier is the diagonal line that splits the happy and the sad points. The equation of this line is xaack = xbeep (or equivalently, xaack – xbeep = 0), because the line corresponds to all the points where the horizontal and the vertical coordinates are equal. The happy zone is the zone in which the number of appearances of aack is greater than or equal to the number of appearances of beep, and the sad zone is the zone in which the number of appearances of aack is less than that of beep.
The diagonal boundary makes the scoring rule visible: sentences with more “aack” fall on one side, and those with more “beep” fall on the other. section 5.1
With more features, the rule is the same: multiply each feature by its weight, add the products and a bias, then apply a step function. In symbols, prediction = step(w₁x₁ + … + wₙxₙ + b). The step function returns 0 for a negative score and 1 otherwise. For two features, the boundary is a line; in higher dimensions, it is a hyperplane. The bias shifts the boundary and determines the prediction when all feature values are zero. section 5.1 section 5.6
How can the boundary improve? The perceptron algorithm starts with a random classifier and repeats a small correction:
- Pick a point. If its category is predicted correctly, leave the classifier alone. section 5.3
- If it is misclassified, adjust the classifier so its boundary moves toward that point. Repeat with other points and measure error to decide when to stop. section 5.3
A line cannot perfectly separate every dataset, so evaluate how many points the classifier gets wrong rather than assuming a perfect split. The same yes-or-no pattern can help frame spam filtering and recommendations. Perceptrons also serve as building blocks of neural networks, where activation functions can differ from the step function used for this classifier. section 5.3 section 5.5 section 10.6




