Handwritten digit recognition is a classic machine learning problem, judging numbers by recognizing handwritten pictures
Because the number category is 0-9, it is a very type problem
This article takes KNN algorithm as an example to realize the recognition of handwritten digits
Article Directory
Low-dimensional handwritten digit recognition
sklearn has its own handwritten digits data set, which is called by datasets.load_digits()
Introduction to load_digits
The digits data set returned by load_digits has 1797 data, and the dimension of the data is 64
digits is a dictionary-like object of type Bunch, we can use the index to call it
transfer
from sklearn import datasets
digits = datasets.load_digits()
index
digits has 5 parts:
data : data, where each element is a 64-dimensional vector
images : images, where each element is an 8×8 matrix
target : the label corresponding to each data
target_names : all category labels
Take the 0th element as an example:
A 64-dimensional vector
8×8 matrix, you can roughly see the outline of the number '0'.
Use plt.imshow() to visualize the images.
Data category labels
. The prediction results of the model:
Use KNN for training and prediction
Divide the data set, use the training set to train knn, and then use the test set to test performance
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
X_train, X_test, y_train, y_test = train_test_split(digits['data'], digits['target'])
knn = KNeighborsClassifier()
knn.fit(X_train, y_train)
knn.score(X_test, y_test)
The score function returns the correct rate of the model on the test set, in the form of:
model.score(X_test, y_test)
It can be seen that the correct rate is very high:
High-dimensional handwritten digit recognition
Import data set
Import a mnist.npy file from the local
import numpy as np
x_train, x_test, y_train, y_test = np.load('data/mnist/mnist.npy', allow_pickle = True)
The shape of the training set: The shape of the
test set:
reshape data set
Check the shape of the data, you can see that each data is a 28×28 matrix
Because it is not possible to directly perform operations on the matrix, it is necessary to convert the matrix to a vector of 784
# reshape训练集
n_samples, n1, n2 = x_train.shape
x_train = x_train.reshape(n_samples, n1*n2).astype(np.float32)
# reshape测试集
n_samples_test, n1_test, n2_test = x_test.shape
x_test = x_test.reshape(n_samples_test, n1_test*n2_test).astype(np.float32)
At this time, each data becomes a vector of 784:
Feature dimensionality reduction
Because the training set has 60,000 data, it takes a long time, you can reduce the dimensionality to reduce the running time
Use PCA principal component analysis to reduce the dimension to 64 dimensions
# 特征降维
from sklearn.decomposition import PCA
pca = PCA(n_components = 64)
decom_x_train = pca.fit_transform(x_train)
decom_x_test = pca.transform(x_test)
Because the pca.transform in line 5 was written as pca.fit_transform before, the accuracy rate was very low, so it will be fine after modification
The correct rate on the test set: