From 00483aba6979b3c3c7b2b8bfc1aec7a1cd741589 Mon Sep 17 00:00:00 2001 From: 202210423105 <3374617300@qq.com> Date: Wed, 16 Oct 2024 22:20:24 +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 | 68 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/function.py b/function.py index b1f8c16..906145a 100644 --- a/function.py +++ b/function.py @@ -25,8 +25,8 @@ import os """ 请在这里填写学号和姓名,不填写即成绩无效。 """ -id = "202210423xxx" -name = "test_name" +id = "202210423105" +name = "魏明帅" """ @@ -54,8 +54,23 @@ def classify(input_vector, dataSet, labels, k=0): Returns: - int: 预测的分类标签。 """ - - + """ + 欧式距离的 kNN 分类算法。 + """ + # 计算欧式距离 + distances = np.linalg.norm(dataSet - input_vector, axis=1) + + # 获取距离最近的 k 个样本的索引 + k_indices = np.argsort(distances)[:k] + + # 获取最近邻样本的标签 + k_nearest_labels = [labels[i] for i in k_indices] + + # 找出出现次数最多的标签 + most_common = max(set(k_nearest_labels), key=k_nearest_labels.count) + + return most_common + # 这里也写自己的代码实现 def load_dataset(directory): """ @@ -68,7 +83,24 @@ def load_dataset(directory): - np.array: 特征数据。 - np.array: 标签数据。 """ - +features = [] +labels = [] + + # 遍历文件夹中的所有文件 + for filename in os.listdir(directory): + if filename.endswith(".txt"): # 假设文件是 .txt 格式 + filepath = os.path.join(directory, filename) + + # 假设文件名格式为 "数字_编号.txt" + label = int(filename.split('_')[0]) + + # 加载文件中的二进制数据并转换为 1D 向量 + image_vector = img_to_vector(filepath) + + features.append(image_vector) + labels.append(label) + + return np.array(features), np.array(labels) # 这里还写自己的代码实现 def img_to_vector(filename): @@ -82,7 +114,11 @@ def img_to_vector(filename): Returns: - np.array: 二值图像向量。 """ - + with open(filename) as file: + lines = file.readlines() + image_vector = np.array([int(char) for line in lines for char in line.strip()]) + + return image_vector if __name__ == "__main__": # 以下代码可用于测试,可自行随意修改 @@ -94,32 +130,32 @@ if __name__ == "__main__": "00000000000011111100000000000000" "00000000001111111110000000000000" "00000000001111111110000000000000" - "00000000001111111100000000000000" + "00000000001111000000000" "00000000011111111000000000000000" - "00000000111111111000000000000000" + "000000001111111110000000000000" "00000000111111110000000000000000" "00000000111111110000000000000000" + "0000000011111111000000000000" "00000000111111110000000000000000" - "00000000111111110000000000000000" - "00000000111111100000000000000000" + "0000000011111110000000000" "00000001111111100000000000000000" "00000001111111100000000000000000" "00000001111111100000000000000000" "00000001111111111111000000000000" - "00000001111111111111111000000000" + "0000000111111111000000000" "00000001111111111111111100000000" "00000011111111111111111110000000" "00000011111111111111111110000000" - "00000001111111111111111110000000" + "0000000111111110000000" "00000001111111111111111110000000" "00000000111111111100111111100000" "00000000111111111000111111100000" - "00000000011111111001111111100000" - "00000000011111111111111111000000" + "000000000111111110011100000" + "000000000111111111111000000" "00000000001111111111111111000000" - "00000000000111111111111111000000" + "00000000000111111111000000" "00000000000011111111111100000000" - "00000000000001111111110000000000" + "000000001110000000000" "00000000000000000111100000000000" ) predicted_label = classify(