更新 function.py
Gitea Actions Challenge Test / Challenge-Actions (push) Successful in 11s

This commit is contained in:
2024-10-17 16:29:41 +08:00
parent 5eaeb5cc9a
commit 988b90d106
+16 -4
View File
@@ -1,6 +1,6 @@
# function.py
import numpy as np
from collections import Counter
"""
挑战说明:
- 自行编写实现 KNN 分类算法。
@@ -25,8 +25,8 @@ import numpy as np
"""
请在这里填写学号和姓名,不填写即成绩无效。
"""
id = "202210423xxx"
name = "test_name"
id = "202210423629"
name = "Tanjunfeng"
"""
@@ -54,7 +54,19 @@ def classify0(inX, dataSet, labels, k=0):
Returns:
- int: 预测的分类标签。
"""
# 计算输入向量与训练集中每一个样本的欧式距离
distances = np.sqrt(np.sum((dataSet - inX) ** 2, axis=1))
# 通过对距离进行升序排序并选择前k个最近距离的索引
nearest_neighbors_indices = distances.argsort()[:k]
# 收集k个最近距离的样本的标签
nearest_labels = [labels[i] for i in nearest_neighbors_indices]
# 使用计数器查找出现频率最高的标签
most_common_label = Counter(nearest_labels).most_common(1)[C_0]()[C_0]()
return most_common_label
def load_data(file_path):
"""