From 483f2bdb945bf212dc62b51143698ba907851511 Mon Sep 17 00:00:00 2001 From: 43606070 <43606070@qq.com> Date: Sun, 20 Oct 2024 11:37:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20function.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.py | 56 ++++++++++++++--------------------------------------- 1 file changed, 15 insertions(+), 41 deletions(-) diff --git a/function.py b/function.py index dbf400c..dacd14c 100644 --- a/function.py +++ b/function.py @@ -77,45 +77,19 @@ if __name__ == "__main__": # 加载训练数据集 train_data, train_labels = load_data("trainingDigits") # 使用正确的函数名 load_data - # 加载测试样本 (此处为简单测试,实际使用时应替换为完整的测试集) - test_data = ( - "00000000000000110000000000000000" - "00000000000011111000000000000000" - "00000000000011111100000000000000" - "00000000001111111110000000000000" - "00000000001111111110000000000000" - "00000000001111111100000000000000" - "00000000011111111000000000000000" - "00000000111111111000000000000000" - "00000000111111110000000000000000" - "00000000111111110000000000000000" - "00000000111111110000000000000000" - "00000000111111110000000000000000" - "00000000111111100000000000000000" - "00000001111111100000000000000000" - "00000001111111100000000000000000" - "00000001111111100000000000000000" - "00000001111111111111000000000000" - "00000001111111111111111000000000" - "00000001111111111111111100000000" - "00000011111111111111111110000000" - "00000011111111111111111110000000" - "00000001111111111111111110000000" - "00000001111111111111111110000000" - "00000000111111111100111111100000" - "00000000111111111000111111100000" - "00000000011111111001111111100000" - "00000000011111111111111111000000" - "00000000001111111111111111000000" - "00000000000111111111111111000000" - "00000000000011111111111100000000" - "00000000000001111111110000000000" - "00000000000000000111100000000000" - ) - - # 将测试样本转换为向量,并进行分类 - predicted_label = classify(np.array([int(char) for char in test_data]).reshape(1, 1024), - train_data, train_labels) - - print(f"预测的标签: {predicted_label}") + # 加载测试数据集 + test_data, test_labels = load_data("testDigits") # 加载测试集文件夹中的所有数据 + + # 测试集预测结果 + correct_count = 0 + total_count = len(test_labels) + + for i in range(total_count): + predicted_label = classify(test_data[i], train_data, train_labels) + print(f"测试样本 {i+1} - 真实标签: {test_labels[i]}, 预测标签: {predicted_label}") + if predicted_label == test_labels[i]: + correct_count += 1 + + accuracy = correct_count / total_count + print(f"分类精度: {accuracy * 100:.2f}%")