From 744e0bfa7f5fef2d667bb108dfcdfc039828d6af Mon Sep 17 00:00:00 2001 From: yuhaopan <1159526601@qq.com> Date: Thu, 26 Sep 2024 17:57:44 +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 | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/function.py b/function.py index df4d153..9b96ddc 100644 --- a/function.py +++ b/function.py @@ -1,6 +1,7 @@ # function.py import numpy as np from numpy.linalg import inv, det +from scipy.stats import mode """ 挑战说明: - 自行编写实现 KNN 分类算法。 @@ -38,7 +39,7 @@ name = "潘宇豪" # 这里写自己的代码实现 -def classify0(inX, dataSet, labels, k=3): +def classify0(inX, dataSet, labels, ,k_values = [3,9]): """ KNN 分类算法。 @@ -52,32 +53,29 @@ def classify0(inX, dataSet, labels, k=3): - int: 预测的分类标签。 """ + def mahalanobis_distance(inX, data, cov_inv): + diff = inX - data + return np.sqrt(np.dot(np.dot(diff, cov_inv), diff.T)) + mean = np.mean(dataSet, axis=0) cov_inv = inv(np.cov(dataSet, rowvar=False)) - # 计算马氏距离 - distances = [] - for data in dataSet: - diff = inX - data - distance = np.sqrt(np.dot(np.dot(diff, cov_inv), diff.T)) - distances.append(distance) - - distances = np.array(distances) - sortedDistIndices = distances.argsort() + # 计算所有k值的预测 + predictions = [] + for k in k_values: + distances = [mahalanobis_distance(inX, data, cov_inv) for data in dataSet] + sortedDistIndices = np.argsort(distances) + classCount = {} + for i in range(k): + voteIlabel = labels[sortedDistIndices[i]] + classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1 + + sortedClassCount = sorted(classCount.items(), key=lambda item: item[1], reverse=True) + predictions.append(sortedClassCount[0][0]) - # 距离加权投票 - classCount = {} - for i in range(k): - index = sortedDistIndices[i] - voteIlabel = labels[index] - weight = 1 / (distances[index] + 1e-5) # 使用距离的倒数作为权重,避免除以零 - classCount[voteIlabel] = classCount.get(voteIlabel, 0) + weight - - # 返回权重最高的类别 - sortedClassCount = sorted(classCount.items(), key=lambda item: item[1], reverse=True) - return sortedClassCount[0][0] - - + # 多数投票机制 + final_prediction, _ = mode(predictions) + return final_prediction.mode[0] def load_data(file_path): """