K nearest neighbor algorithm

Machine learning algorithms generate models from data, that is, algorithms for learning. We feed experience to the algorithm, and it can generate models from that experience. When faced with a new situation, the model provides us with the predicted results. For example, when identifying numbers and words, it is not necessary to recognize them by color, but by binary images, which are composed of 0 and 1. Machine learning will match the most similar words or numbers according to the position of 0 and 1, so as to obtain the results. The K-nearest neighbor algorithm in machine learning is most suitable for recognizing text or digital information in images.

K nearest neighbor algorithm, also known as KNN algorithm, is a very classical machine learning algorithm. Its principle is very simple: for a new sample, it can be understood as a new digital image or text images, K neighbor algorithm can find the most similar with it in the existing data K data, or the nearest K data, if this K data most belong to a category, then the sample also belong to this category.

Identification Numbers

In the first opencv-Python development guide we introduced binary images, which can distinguish between shapes and general Outlines of objects. As shown below:

So the picture A here is A matrix of 0s and 1s, where the number 1 is where the colors are, and the number 0 is where the colors are not.

Here, the sample data we provide to machine learning is a one-dimensional array of 1024 elements, provided by Excel, while the image is a matrix rather than a one-dimensional array. Therefore, when processing the original image, we need to convert the matrix data of the image into a one-dimensional array, so as to facilitate the matching prediction of machine learning.

Adjust the image

First of all, the digital image we need to identify may not be a binary image, or even a grayscale image. So we need to convert it to a binary image.

Second, the binary image transformed by OpenCV is a matrix, while the data trained by machine learning is a one-dimensional array of 1024 lengths. So, we also need to reduce the image to a 32 by 32 pixel image so that it can be converted into a one-dimensional array of 1024 0,1 data.

The specific code is as follows:

import cv2

img = cv2.imread("40.jpg")
img = cv2.resize(img, (32.32))
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
t, img = cv2.threshold(img, 127.255, cv2.THRESH_BINARY_INV)
img[img == 255] = 1
img_array= img.reshape(1, -1)  Convert to a one-dimensional array
Copy the code

Here, we first take the image, and then convert it to a size of 32 by 32 pixels. Then, the image is converted to grayscale image, and the image is changed to 0 and 255 values through binarization processing, and finally the white part of 255 is replaced by 1. Finally, it is converted to a one-dimensional array.

K nearest neighbor algorithm model construction

Whether it is K nearest neighbor algorithm or machine learning algorithm, we generally build machine learning model is divided into two steps. The first step is to divide the training set and test set, and the second step is to build the model.

The following is our specific implementation, the code is as follows:

import cv2
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier as KNN

df = pd.read_excel("Handwriting recognition. XLSX")
When extracting feature variables and identifying numbers, the feature is 1024 0,1 data, and the target variable is 1024 numbers to form the corresponding result numbers
X = df.drop(columns="Corresponding number")
Y = df['Corresponding number']

x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=111)
knn = KNN(n_neighbors=5)
knn.fit(x_train, y_train)

answer=knn.predict(img_array)
print("The numbers in the picture are:"+str(answer[0]))
Copy the code

Here, we first read the handwritten font recognition data set, and then extract feature variables and target variables.

Then, the train_test_split function is used to divide the acquired data set into test set and training set. Test_size =0.2 means that 20% of the data is divided into test set. The training set returns X_train and Y_train, and the test set returns X_test and y_test.

Then, the training set data is used to model FIT, where THE K-neighbor algorithm n_neighbors=5 indicates that five neighbor points are selected to determine the classification of digital images, or recognition judgment.

After the modeling is completed, the one-dimensional array of the above transformed images can be directly substituted into knN. predict function to obtain the predicted results. The pictures we tested are as follows:

After running, the result is as follows:

Training documentation: Click download