generated from LufsX/Experiment-kNN
This commit is contained in:
+4
-2
@@ -56,8 +56,10 @@ def classify(input_vector, dataSet, labels, k=3):
|
||||
# 获取距离最近的k个样本的标签
|
||||
nearest_neighbors = np.argsort(distances)[:k]
|
||||
nearest_labels = labels[nearest_neighbors]
|
||||
# 转换 nearest_labels 为 Python 列表
|
||||
nearest_labels = nearest_labels.tolist()
|
||||
|
||||
# 确保 nearest_labels 是一维的,转换为 Python 列表
|
||||
nearest_labels = nearest_labels.flatten().tolist()
|
||||
|
||||
# 投票,返回出现次数最多的标签
|
||||
most_common = Counter(nearest_labels).most_common(1)
|
||||
return most_common[0][0]
|
||||
|
||||
Reference in New Issue
Block a user