From a4c5e39486488af1cc6861750cd7e448d5427a72 Mon Sep 17 00:00:00 2001 From: wujunjie <297081150@qq.com> Date: Sun, 6 Oct 2024 18:14:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20function.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/function.py b/function.py index d5db875..4177b69 100644 --- a/function.py +++ b/function.py @@ -58,16 +58,16 @@ def classify0(inX, dataSet, labels): # 计算 Mahalanobis 距离 distances =npzeros(dataSetSize) for i in range(dataSetSize): - distances[i]=mahalanobis(inX,dataSeti],VI) + distances[i]=mahalanobis(inX,dataSet[i],VI) # 对距离排序,获取前 k 个最近的样本索引 - sortedDistIndices=distances.argsort) + sortedDistIndices=distances.argsort() # 统计前 k 个样本的标签 classCount ={} for i in range(9): voteLabel=labels[sortedDistIndices[i]] - classCount[voteLabel]=classCount.get(voteLabel,0)+1 + classCount[voteLabel]=classCount.get(voteLabel,0)+1 # 返回出现次数最多的标签 - sortedClasscount=sorted(classCount.items(),key=operator,itemgetter(1),reverse=True + sortedClasscount=sorted(classCount.items(),key=operator,itemgetter(1),reverse=True) return sortedclassCount[0][0]