From 667941dcfad971b2a17234a78dbb58a35d90594b Mon Sep 17 00:00:00 2001 From: jack <2170067548@qq.com> Date: Wed, 16 Oct 2024 22:13:44 +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 | 90 ++++++++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/function.py b/function.py index b1f8c16..b752d98 100644 --- a/function.py +++ b/function.py @@ -25,8 +25,8 @@ import os """ 请在这里填写学号和姓名,不填写即成绩无效。 """ -id = "202210423xxx" -name = "test_name" +id = "202210423102" +name = "周佳成" """ @@ -41,7 +41,7 @@ name = "test_name" # 这里写自己的代码实现 -def classify(input_vector, dataSet, labels, k=0): +def classify(input_vector, dataSet, labels, k=3): """ KNN 分类算法。 @@ -54,6 +54,14 @@ def classify(input_vector, dataSet, labels, k=0): Returns: - int: 预测的分类标签。 """ + if k > len(labels): + raise ValueError("k 不能大于训练集样本数") + + distances = np.linalg.norm(dataSet - input_vector, axis=1) + sorted_indices = distances.argsort() + nearest_labels = np.array(labels)[sorted_indices[:k]] + predicted_label = np.bincount(nearest_labels).argmax() + return predicted_label # 这里也写自己的代码实现 @@ -68,6 +76,17 @@ def load_dataset(directory): - np.array: 特征数据。 - np.array: 标签数据。 """ + data = [] + labels = [] + for filename in os.listdir(directory): + if filename.endswith(".txt"): + # 提取文件名中的标签信息 + label = int(filename.split('_')[0]) + labels.append(label) + # 将图像文件转换为向量 + vector = img_to_vector(os.path.join(directory, filename)) + data.append(vector) + return np.array(data), np.array(labels) # 这里还写自己的代码实现 @@ -82,50 +101,31 @@ def img_to_vector(filename): Returns: - np.array: 二值图像向量。 """ - + with open(filename, 'r') as f: + lines = f.readlines() + # 将32x32的字符矩阵转换为长度为1024的一维数组 + vector = np.array([int(char) for line in lines for char in line.strip()]) + return vector if __name__ == "__main__": - # 以下代码可用于测试,可自行随意修改 - train_data, train_labels = load_dataset("trainingDigits") + # 加载训练集和测试集 + train_data, train_labels = load_dataset("D:/二值手写数字数据集/trainingDigits") + test_data, test_labels = load_dataset("D:/二值手写数字数据集/testDigits") - 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, + # 记录预测的正确数量 + correct_count = 0 + + # 遍历测试集,逐个样本进行分类 + for i in range(len(test_data)): + 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 / len(test_data) * 100 + print(f"测试集的准确率: {accuracy:.2f}%") ) - print(f"预测的标签: {predicted_label}") +