import numpy as np
import matplotlib.pyplot as plt
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.externals import joblib
# 查看一下数据集的数据
# zero = plt.imread('./knn_num_data/0/0_1.bmp')
# plt.imshow(zero,cmap='gray')
# print(zero.shape)
# 将数据组合成可以训练的数据集
path = './knn_num_data/%d/%d_%d.bmp'
data = []
target = []
for i in range(10):
for j in range(500):
im_data = plt.imread(path % (i, i, j + 1))
data.append(im_data)
target.append(i)
data = np.array(data)
# print(data.shape)
# knn只能用二维数据 所以更改一下shape
data_ = data.reshape(5000, -1)
# print(data_.shape)
# 分割数据集 选取1%作为测试数据集
X_train, X_test, y_train, y_test = train_test_split(data_, target, test_size=0.01)
# 实例化KNN分类器
knn = KNeighborsClassifier()
knn.fit(X_train, y_train)
# 模型保存路径
save_path_name = 'knn_train_mode
KNN实现手写数字的识别
最新推荐文章于 2024-06-29 18:29:15 发布
这篇博客介绍如何利用KNN(K-Nearest Neighbors)算法对手写数字进行识别,通过链接提供了一个数据集,读者可以借此实践算法应用。
摘要由CSDN通过智能技术生成