From 17ec7e2b3ee88476f6bc9cc3d4454cad810fdee4 Mon Sep 17 00:00:00 2001 From: wujunjie <297081150@qq.com> Date: Thu, 26 Sep 2024 10:22:22 +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 | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/function.py b/function.py index 89b7dc3..17ffcb6 100644 --- a/function.py +++ b/function.py @@ -23,8 +23,8 @@ import numpy as np """ 请在这里填写学号和姓名,不填写即成绩无效。 """ -id = "202210423xxx" -name = "test_name" +id = "202210423217" +name = "吴俊煯" """ @@ -50,7 +50,15 @@ def classify0(inX, dataSet, labels): Returns: - int: 预测的分类标签。 """ - + distances = [np.sqrt(np.sum((inX - x) ** 2)) for x in dataSet] + + k_indices = np.argsort(distances)[:k] + k_nearest_labels = [labels[i] for i in k_indices] + + from collections import Counter + vote_result = Counter(k_nearest_labels).most_common(1) + + return vote_result[0][0] def load_data(file_path): """