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

This commit is contained in:
cy
2024-10-17 20:08:53 +08:00
parent 995f9fc7d5
commit 6fdfe0b65c
+1 -4
View File
@@ -41,7 +41,6 @@ def classify(input_vector, dataSet, labels, k=2):
Returns:
- int: 预测的分类标签。
"""
sigma=1.0
dataSet_size = dataSet.shape[0]
# 计算欧氏距离
diff_mat = np.tile(input_vector, (dataSet_size, 1)) - dataSet
@@ -53,9 +52,7 @@ def classify(input_vector, dataSet, labels, k=2):
class_count = {}
for i in range(k):
vote_label = labels[sorted_indices[i]]
# 采用高斯核函数计算权重
weight = np.exp(- sq_distances[sorted_indices[i]] / (2 * sigma ** 2))
class_count[vote_label] = class_count.get(vote_label, 0) + weight
class_count[vote_label] = class_count.get(vote_label, 0) + 1
sorted_class_count = sorted(class_count.items(), key=lambda x: x[1], reverse=True)
return sorted_class_count[0][0]