From 7da14e4135d13b415b25497335a7564afc5499fe Mon Sep 17 00:00:00 2001 From: dong <2938690875@qq.com> Date: Thu, 10 Oct 2024 16:27:17 +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 | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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):