diff --git a/function.py b/function.py index 4177b69..a31aea5 100644 --- a/function.py +++ b/function.py @@ -56,7 +56,7 @@ def classify0(inX, dataSet, labels): # 计算数据集的协方差矩阵的逆 VI = np.linalg.inv(np.cov(dataSet.T)) # 计算 Mahalanobis 距离 - distances =npzeros(dataSetSize) + distances =np.zeros(dataSetSize) for i in range(dataSetSize): distances[i]=mahalanobis(inX,dataSet[i],VI) # 对距离排序,获取前 k 个最近的样本索引 @@ -67,7 +67,9 @@ def classify0(inX, dataSet, labels): voteLabel=labels[sortedDistIndices[i]] 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]