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

This commit is contained in:
cy
2024-10-15 20:48:42 +08:00
parent d3bce57a27
commit 0f89bbabd8
+3 -1
View File
@@ -52,7 +52,9 @@ def classify(input_vector, dataSet, labels, k=2):
class_count = {}
for i in range(k):
vote_label = labels[sorted_indices[i]]
class_count[vote_label] = class_count.get(vote_label, 0) + 1
# 计算权重,这里采用简单的距离倒数作为权重
weight = 1 / distances[sorted_indices[i]]
class_count[vote_label] = class_count.get(vote_label, 0) + weight
sorted_class_count = sorted(class_count.items(), key=lambda x: x[1], reverse=True)
return sorted_class_count[0][0]