当前位置:   article > 正文

KNN 手写字体识别_手写字体识别.xlsx

手写字体识别.xlsx

KNN手写数字的代码实现

1.读取数据

# 1.读取数据
import pandas as pd

df = pd.read_excel('./手写字体识别.xlsx')
df.head()
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述

2.提取特征变量和目标变量

X = df.iloc[:,1:]
y = df.iloc[:,0]
  • 1
  • 2

3.划分训练集和测试集

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=27)
X_train.shape, X_test.shape
y_train.shape, y_test.shape
  • 1
  • 2
  • 3
  • 4
  • 5

((1547, 1024), (387, 1024))
((1547,), (387,))

4.模型搭建

from sklearn.neighbors import KNeighborsClassifier as KNN

knn = KNN(n_neighbors=5)
knn.fit(X_train, y_train)
  • 1
  • 2
  • 3
  • 4

KNeighborsClassifier(algorithm=‘auto’, leaf_size=30, metric=‘minkowski’,
metric_params=None, n_jobs=None, n_neighbors=5, p=2,
weights=‘uniform’)

5.模型预测与评估

y_pred = knn.predict(X_test)
y_pred[:100]
  • 1
  • 2

array([5, 3, 7, 8, 9, 2, 1, 4, 5, 8, 9, 5, 9, 3, 3, 2, 3, 7, 9, 1, 0, 0,
7, 6, 6, 7, 0, 9, 6, 9, 1, 8, 6, 9, 2, 5, 2, 4, 5, 8, 3, 6, 9, 4,
9, 2, 7, 3, 4, 9, 5, 6, 7, 3, 3, 8, 3, 1, 5, 3, 6, 7, 5, 0, 3, 7,
1, 4, 9, 1, 5, 1, 2, 6, 9, 1, 9, 5, 5, 9, 2, 8, 8, 4, 4, 9, 4, 3,
9, 8, 0, 3, 4, 3, 6, 8, 5, 2, 9, 0], dtype=int64)

6.真实预测值对比

arr = pd.DataFrame({'实际值': list(y_test), '预测值': list(y_pred)})
arr.head(10)
  • 1
  • 2

在这里插入图片描述

7.查看模型准确度

from sklearn.metrics import accuracy_score

score = accuracy_score(y_pred, y_test)
score
  • 1
  • 2
  • 3
  • 4

0.979328165374677

score = knn.score(X_test, y_test)
score
  • 1
  • 2

0.979328165374677

8.图片大小调整及显示

from PIL import Image

img = Image.open('./数字5.jpg')
img = img.resize((32, 32))
# img.show()
img
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在这里插入图片描述

9.图片灰度处理

img = img.convert('L')
# img.show()
img
  • 1
  • 2
  • 3

在这里插入图片描述

10.图片二值化处理

import numpy as np

img_new = img.point(lambda x:0 if x>128 else 1)
arr = np.array(img_new)
for i in range(arr.shape[1]):
    print(arr[i])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0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 1 1 1 1 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 1 1 1 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

11.将2维数组转为1维

arr_new = arr.reshape(1, -1)
arr_new.shape
  • 1
  • 2

(1, 1024)

12.利用训练好的模型进行预测

pred_digit = knn.predict(arr_new)
pred_digit[0]
  • 1
  • 2

5

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/1008027
推荐阅读
相关标签
  

闽ICP备14008679号