More Than Two Classes
Reading handwritten digits.
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.

- 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.
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))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.
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