AI Hints
Home›Machine Learning Tutorial›More Classifiers
Machine Learning · Lesson 32 of 85

More Than Two Classes

Reading handwritten digits.

⏱ 2 min read▶ Video at 1:09:07🎓 Free
Watch this lesson in the Machine Learning courseWatch on YouTube ↗Full Machine Learning course

So far, every answer has been one of two options. But what about reading a handwritten number? There are ten possible answers. This was once a famous, unsolved challenge, and post offices would have paid a fortune to solve it. You will do it in a few lines.

More Than Two Classes
From the video: More Than Two Classes
  • Ten possible answers
  • A once-unsolved problem
  • You will solve it in minutes

Reading digits

This dataset holds small images of handwritten digits, each one eight pixels by eight. So every image is just sixty four numbers. Logistic regression handles ten classes automatically, by giving a chance to each digit, and choosing the largest. Run it. Around ninety seven percent accuracy, from pixels, with a simple linear model. The confusion matrix is now bigger, and the numbers on the diagonal are the correct answers. At the bottom, we see the ten chances for one image, adding up to one.

digits.pypython
from sklearn.datasets import load_digits
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

X, y = load_digits(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, stratify=y, random_state=0)

model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=2000))
pred = model.fit(X_tr, y_tr).predict(X_te)

print("Images:", X.shape[0], " Pixels each:", X.shape[1])
print("Accuracy:", round(accuracy_score(y_te, pred), 3))
print(confusion_matrix(y_te, pred)[:5, :5])
print("Chances for one image:", model.predict_proba(X_te[:1])[0].round(2))
Output
Images: 1797  Pixels each: 64
Accuracy: 0.969
[[45  0  0  0  0]
 [ 0 45  0  0  0]
 [ 0  1 43  0  0]
 [ 0  0  0 46  0]
 [ 0  1  0  0 43]]
Chances for one image: [0.08 0.   0.7  0.21 0.   0.   0.   0.   0.   0.  ]

Three ways to have many classes

There are three types of many-answer problem. Multi class, where each example has exactly one label, like a digit. One versus rest, a technique that trains one yes or no model for each class, and picks the most confident. And multi label, where an example can have several labels at once, such as a film that is both a comedy and a romance.

Multi-classexactly one answer
One-vs-restone model per class
Multi-labelseveral answers at once

Summary

Ten classes, the same ideas. Soon, deep learning will take this much further, in our follow-up course. But logistic regression is not the only way to classify. Next, the simplest algorithm of all, one that does no learning whatsoever.

Key takeaways
  • Many classes: one chance for each
  • The largest chance wins
  • Multi-label allows several answers
Next up: K-Nearest Neighbours — you are like your neighbours