diff --git a/function.py b/function.py index 9611daf..85383ab 100644 --- a/function.py +++ b/function.py @@ -53,6 +53,18 @@ def classify0(inX, dataSet, labels, k=0): Returns: - int: 预测的分类标签。 """ + dataSetSize=dataSet.shape[0] + diffMat=inX - dataset + sqDiffMat=diffMat**2 + sqDistances=sqDiffMat.sum(axis=1) + distance=sqDistances**0.5 + sortedDistIndicies=distances.argsort() + classCount={} + for i in range(k): + votellabel=labels[sortedDistIndicies[i]] + classCount[votellabel]=classCount.get(votellabel,0)+1 + sortedClassCount=sorted(classCount.items(),key=operator.itemgetter(1),reverser=True) + return sortedClassCount[0][0] def load_data(file_path):