Lesson 38 · Data Science Projects
Building and Training Handwritten Digit Recognition Models with Scikit Learn
This lesson will help you understand how computers can recognize handwritten digits using Python. You will see every step from loading real world data to…
- CourseData Science Projects
- Lesson38 of 33
- Video24 min
- FormatJupyter notebook · 16 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbRecognizing HandWritten Digits in Scikit Learn#
- This lesson will help you understand how computers can recognize handwritten digits using Python.
- You will see every step from loading real world data to creating and evaluating a simple machine learning model.
- We use the Digits dataset built into scikit learn, a standard tool for this kind of problem.
- Almost anyone can relate to digits, so it is a friendly way to get into data mining and machine learning.
- Even if you have never used Python before, you will follow along and get results by the end.
- If you are finding this useful, remember to like and subscribe for more beginner friendly videos.
# Suppress warnings for a clean notebook
import warnings; warnings.filterwarnings("ignore")
import numpy as np
np.random.seed(42)
Data setup#
- We use the built in Digits dataset from scikit learn.
- Each digit is an image with 8 by 8 pixels.
- The problem is to predict which digit is shown in an image.
- All data is small, free to use, and needs no download.
# Load the digits dataset from sklearn
from sklearn.datasets import load_digits
digits = load_digits()
X, y = digits.data, digits.target
print(X.shape)
print(y.shape)
# Display the first 5 rows of data
import pandas as pd
df = pd.DataFrame(X)
print(df.head())
# Visualize some sample digit images
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 5, figsize=(10, 3))
for i, ax in enumerate(axes):
ax.imshow(digits.images[i], cmap="gray_r")
ax.set_title(f"Label: {digits.target[i]}")
ax.axis("off")
plt.show()
Why recognize digits?#
- Digit recognition is a classic problem in data science and artificial intelligence.
- It connects to real world things like sorting post, bank checks, or reading utility meters.
- Successful digit recognition is the first step towards reading handwriting or even documents.
- Machine learning is the tool we use to teach computers to spot digit patterns.
# Check for missing data
import numpy as np
missing = np.isnan(X).sum()
print(f"Total missing values: {missing}")
# Split the data into train and test sets
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
print(X_train.shape, X_test.shape)
# Look at the label distribution in the training set
unique, counts = np.unique(y_train, return_counts=True)
for value, count in zip(unique, counts):
print(f"Digit {value}: {count} samples")
# Scale the features for better model performance
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
Building your first digit classifier#
- The most basic classifier is called logistic regression.
- It is a simple algorithm that learns boundaries between groups.
- Logistic regression works for digits because it can handle numbers as categories.
- The steps below will fit a model to the training digits.
# Train a logistic regression model
from sklearn.linear_model import LogisticRegression
clf = LogisticRegression(max_iter=1000, solver="lbfgs", multi_class="auto", random_state=42)
clf.fit(X_train_scaled, y_train)
# Predict on the test set
y_pred = clf.predict(X_test_scaled)
print(y_pred[:10])
# Measure model accuracy
from sklearn.metrics import accuracy_score
acc = accuracy_score(y_test, y_pred)
print(f"Accuracy: {acc:.2f}")
# Show model performance for each digit
from sklearn.metrics import classification_report
print(classification_report(y_test, y_pred))
# Visualize results: show predictions vs actuals
fig, axes = plt.subplots(2, 5, figsize=(12, 5))
for i, ax in enumerate(axes.flat):
idx = i
ax.imshow(X_test[idx].reshape(8,8), cmap="gray_r")
ax.set_title(f"Pred:{y_pred[idx]}, True:{y_test[idx]}")
ax.axis("off")
plt.tight_layout()
plt.show()
Bonus: Try your own digit#
- Did you know you can predict your own data if it matches the digits format?
- Just reshape your 8 by 8 gray pixel image to a 64 element array.
- Feed it through the scaler and model, and see what comes back.
- This is a mini project you can try for practice after the video.
- If you want more beginner friendly projects, do not forget to subscribe.
# Confusion matrix: where does the model get confused?
import seaborn as sns
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(8,6))
sns.heatmap(cm, annot=True, fmt="d", cmap="YlGnBu")
plt.xlabel("Predicted")
plt.ylabel("True")
plt.title("Confusion Matrix")
plt.show()
# Try a different model: K Nearest Neighbors
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train_scaled, y_train)
knn_pred = knn.predict(X_test_scaled)
knn_acc = accuracy_score(y_test, knn_pred)
print(f"KNN Accuracy: {knn_acc:.2f}")
# Time series: visualize digit counts over index
import matplotlib.pyplot as plt
digits_per_index = pd.Series(y).value_counts().sort_index()
plt.figure(figsize=(8,4))
plt.plot(digits_per_index.index, digits_per_index.values, marker="o")
plt.title("Sample Count By Digit Value")
plt.xlabel("Digit")
plt.ylabel("Count")
plt.show()
Congratulations! You built a digit recognizer.#
- You saw practical machine learning from start to finish.
- Using scikit learn, you loaded, visualized, split, scaled, and modeled data.
- You picked a model and verified its predictions.
- You learned about confusion matrices, model accuracy, and alternative algorithms.
- This is a foundation for all kinds of pattern recognition tasks.
- Do not forget to practice and try tweaks.
- See the YouTube video description for more exercises and helpful resources.
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



