generated from LufsX/Challenge-kNN
chore: try to opt runing time
Gitea Actions Challenge Test / Challenge-Actions (push) Successful in 12s
Gitea Actions Challenge Test / Challenge-Actions (push) Successful in 12s
This commit is contained in:
+2
-2
@@ -60,10 +60,10 @@ def classify0(inX, dataSet, labels, k=8):
|
|||||||
distances = np.sqrt(np.sum(diff @ VI * diff, axis=1))
|
distances = np.sqrt(np.sum(diff @ VI * diff, axis=1))
|
||||||
|
|
||||||
# 对距离排序,获取前 k 个最近的样本索引
|
# 对距离排序,获取前 k 个最近的样本索引
|
||||||
sortedDistIndices = np.argsort(distances)
|
sortedDistIndices = np.argsort(distances)[:k]
|
||||||
|
|
||||||
# 统计前 k 个样本的标签
|
# 统计前 k 个样本的标签
|
||||||
classCount = np.bincount(labels[sortedDistIndices[:k]])
|
classCount = np.bincount(labels[sortedDistIndices], minlength=np.max(labels) + 1)
|
||||||
|
|
||||||
# 返回出现次数最多的标签
|
# 返回出现次数最多的标签
|
||||||
return np.argmax(classCount)
|
return np.argmax(classCount)
|
||||||
|
|||||||
Reference in New Issue
Block a user