请完善以下代码,用knn算法构建图像识别网络,完成训练程序和测试程序两个程序,输出训练图像和测试正确率,其中训练数据包含15种,每种25个,合计3750个样本,图像大小为28*28的灰度图像。import numpy as np import cv2 import os dirPath = "./testDataOrg/" totNum=0 if os.path.isdir(dirPath): fileList = os.listdir(dirPath) for f in fileList: nn1 = f.rfind(',')+1 nn2 = f.find('}',nn1) label = int(f[nn1:nn2])#图像标签 1到11对应”零“到”十“,12到15对应”百“,”千“,”万“,”亿“。 print(f, label) totNum += 1 fi = dirPath+f img = cv2.imread(fi)#图像数据 cv2.imshow("img", cv2.resize(img, (300, 300))) cv2.waitKey(0) print("图像数量:",totNum)
时间: 2024-03-28 17:39:36 浏览: 46
knn算法实现的数字识别包括训练集和测试集和代码
import numpy as np
import cv2
import os
# 定义knn算法
def knn(train_data, train_labels, test_data, k):
# 计算欧式距离
dists = np.sum((train_data - test_data)**2, axis=1)**0.5
# 根据距离排序,选取前k个
inds = np.argsort(dists)[:k]
# 统计前k个样本的标签
counts = np.bincount(train_labels[inds])
# 返回出现次数最多的标签
return np.argmax(counts)
# 加载数据集
dirPath = "./testDataOrg/"
totNum = 0
train_data = []
train_labels = []
test_data = []
test_labels = []
if os.path.isdir(dirPath):
fileList = os.listdir(dirPath)
for f in fileList:
nn1 = f.rfind(',')+1
nn2 = f.find('}',nn1)
label = int(f[nn1:nn2])#图像标签 1到11对应”零“到”十“,12到15对应”百“,”千“,”万“,”亿“。
totNum += 1
fi = dirPath+f
img = cv2.imread(fi, cv2.IMREAD_GRAYSCALE)#图像数据
if totNum % 4 == 0: # 每种图像取其中的1/4作为测试数据
test_data.append(img.flatten())
test_labels.append(label)
else:
train_data.append(img.flatten())
train_labels.append(label)
print("图像数量:",totNum)
# 训练模型
train_data = np.array(train_data)
train_labels = np.array(train_labels)
k = 5 # 选取k=5
correct = 0 # 统计训练集上的正确率
for i in range(train_data.shape[0]):
pred = knn(train_data, train_labels, train_data[i], k)
if pred == train_labels[i]:
correct += 1
print("训练集正确率:", correct / train_data.shape[0])
# 测试模型
test_data = np.array(test_data)
test_labels = np.array(test_labels)
correct = 0 # 统计测试集上的正确率
for i in range(test_data.shape[0]):
pred = knn(train_data, train_labels, test_data[i], k)
if pred == test_labels[i]:
correct += 1
print("测试集正确率:", correct / test_data.shape[0])
阅读全文