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

This commit is contained in:
zhe
2024-10-16 19:30:41 +08:00
parent 5fc0648a2b
commit d96834afcf
+19 -3
View File
@@ -25,8 +25,8 @@ import numpy as np
"""
请在这里填写学号和姓名,不填写即成绩无效。
"""
id = "202210423xxx"
name = "test_name"
id = "202210423111"
name = "李劲"
"""
@@ -54,7 +54,23 @@ def classify0(inX, dataSet, labels, k=0):
Returns:
- int: 预测的分类标签。
"""
inX = np.array(inX)
dataSet = np.array(dataSet)
# 计算输入样本与所有训练样本之间的欧几里得距离
distances = [np.sqrt(np.sum((inX - x_train) ** 2)) for x_train in dataSet]
# 根据距离对训练样本进行排序,并获取前 k 个最近邻的索引
k_indices = np.argsort(distances)[:k]
# 根据索引获取前 k 个最近邻的标签
k_nearest_labels = [labels[i] for i in k_indices]
# 使用 Counter 来找出出现次数最多的标签
most_common = Counter(k_nearest_labels).most_common(1)
# 返回出现次数最多的标签(即预测的分类标签)
return most_common[0][0]
def load_data(file_path):
"""