From 337f3294a3138bafa52748df7647285300fb3aad Mon Sep 17 00:00:00 2001 From: yuhaopan <1159526601@qq.com> Date: Tue, 15 Oct 2024 20:24:06 +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 | 60 +++++++++++++++++++++-------------------------------- 1 file changed, 24 insertions(+), 36 deletions(-) diff --git a/function.py b/function.py index fadf833..05fac37 100644 --- a/function.py +++ b/function.py @@ -38,44 +38,32 @@ name = "潘宇豪" """ -# 这里写自己的代码实现 -def classify0(inX, dataSet, labels, ,k_values = [3,4,5,6,7,8,9]): +def classify0(inX, dataSet, labels, k): """ - KNN 分类算法。 - - 参数: - - inX (numpy array): 输入待分类的样本。 - - dataSet (numpy array): 训练数据集。 - - labels (list or numpy array): 训练集对应的标签。 - - k (int): 使用的邻居数量。 - - 返回值: - - int: 预测的分类标签。 + KNN classification using Mahalanobis distance for fast computation. + inX: The input sample to classify + dataSet: The training data + labels: The corresponding labels for the training data + k: The number of nearest neighbors to consider """ - - 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)) - - # 计算所有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]) - - # 多数投票机制 - final_prediction, _ = mode(predictions) - return final_prediction.mode[0] + # Calculate covariance matrix and its inverse from the dataset + cov_matrix = np.cov(dataSet.T) + inv_cov_matrix = inv(cov_matrix) + + # Compute the Mahalanobis distance between inX and all points in the dataset + diff = dataSet - inX + distances = np.sqrt(np.sum(np.dot(diff, inv_cov_matrix) * diff, axis=1)) + + # Sort by distance and return the indices of the k nearest neighbors + sorted_indices = np.argsort(distances) + + # Get the labels of the k nearest neighbors + k_nearest_labels = [labels[i] for i in sorted_indices[:k]] + + # Return the most common label among the k nearest neighbors + most_common_label = Counter(k_nearest_labels).most_common(1)[0][0] + + return most_common_label def load_data(file_path): """