[Archival version] How to create a handwritten digit recognition AI using Python | Get started with MNIST and scikit-learn!

programming

"It's amazing that AI can be used to identify images, but can I make it myself?"

Have you ever thought that?

In this article, we will show you how anyone can easily create an AI that reads handwritten numbers using Python.of,Easy to understand even for programming beginnersWe explain it.

The following is used:The representative machine learning teaching material "MNIST dataset" and the popular Python library "scikit-learn".

Through this theme, which is the perfect first step in AI development,You will also learn the basics of image recognition and machine learning.

Learn about MNIST

Conclusion: MNIST is the gateway to AI learning

MNIST isA collection of handwritten digits (0-9)It's a great way to learn the basics of machine learning.

Basic information about MNIST

• Image size: 28x28 pixels grayscale

• Number of data: 60,000 for training, 10,000 for testing

• Purpose of use: Mainly evaluating image recognition algorithms

Why MNIST?

• Data is organized and easy to handle

• No complex pretreatment required

• Model training is completed in a short time

• Used by beginners and professionals alike

Reference: Released by the National Institute of Standards and Technology (NIST)

The EMNIST Dataset
What is it?The EMNIST dataset is a set of handwritten character digits derived from the

Preparing the environment with Python and scikit-learn

Conclusion: Beginners can use Anaconda to get started

To set up a Python development environment,It is convenient to use Anaconda.

How to install Python

Official websiteDownload Anaconda from

• Python and core libraries included out of the box

Required Libraries

1
pip install numpy matplotlib scikit-learn

• NumPy: Numerical Computation

• Matplotlib: Graph drawing

• Scikit-learn: Machine learning models

With just this,You can start developing AI right away.

Let's load the MNIST data.

Conclusion: Easy to read with just scikit-learn

MNIST is built into scikit-learn, so there is no need to download it from an external site.

Data reading code

1
from sklearn.datasets import fetch_openml mnist = fetch_openml('mnist_784', version=1) X, y = mnist['data'], mnist['target']

X: Image pixel information (784 dimensions)

• y: Correct answer label (0 to 9)

Let's take a look at the data

1
import matplotlib.pyplot as plt digit = X[0].reshape(28, 28) plt.imshow(digit, cmap='gray') plt.title(f'Label: {y[0]}') plt.show()

Images and labels can be displayed together for intuitive understanding.

Steps to create a handwritten digit recognition AI

Conclusion: High-precision classification is possible with scikit-learn's SVM model

SVM (Support Vector Machine) works well with MNIST.Highly accurate classification resultsYou will get:

Building and training the model

1
from sklearn.svm import SVC 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) model = SVC(gamma=0.05) model.fit(X_train, y_train)

Try making a prediction

1
pred = model.predict([X_test[0]]) print(f"Prediction result: {pred[0]}")

Let's compare the numbers we see with the numbers read by the AI.

How to evaluate the accuracy of a model

Conclusion: Accuracy evaluation reveals the reliability of AI

To know the performance of a model, it is essential to evaluate it using test data.

Code to check accuracy

1
from sklearn.metrics import accuracy_score y_pred = model.predict(X_test) accuracy = accuracy_score(y_test, y_pred) print(f"Accuracy rate: {accuracy:.2f}")

Other indicators to check

• Confusion matrix

• Recall rate/precision rate

• F1 Score

1
from sklearn.metrics import classification_report print(classification_report(y_test, y_pred))

Summary and future applications

Conclusion: Handwriting AI is a great first step

Classification of handwritten digits:It's a great introductory topic to AI.

Next steps

• Also try other classification problems (e.g. cat and dog images)

• Evolution to deep learning (TensorFlow and PyTorch)

• Link with web apps to create services

Copied title and URL