From c67aeae89fca74588cb081ac3f6bc7856da514ea Mon Sep 17 00:00:00 2001 From: Xionghaoran <719929980@qq.com> Date: Tue, 15 Oct 2024 17:38:21 +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 | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/function.py b/function.py index ce6cc91..0669ab1 100644 --- a/function.py +++ b/function.py @@ -59,7 +59,7 @@ def classify(input_vector, dataSet, labels, k=3): # 获取距离最近的 k 个样本的索引 nearest_neighbors_indices = distances.argsort()[:k] # 获取这些样本的标签 - nearest_labels = np.array(labels)[nearest_neighbors_indices] + nearest_labels = labels[nearest_neighbors_indices] # 统计出现次数最多的标签并返回 predicted_label = np.bincount(nearest_labels).argmax() return predicted_label @@ -108,8 +108,10 @@ def img_to_vector(filename): if __name__ == "__main__": + # 测试代码 train_data, train_labels = load_dataset("trainingDigits") - + + # 测试数据(缩减以减少运行时间) test_data = ( "00000000000000110000000000000000" "00000000000011111000000000000000"