From 7ceb6f3decf879ebc6a0c0fa4bb50ab4244a872c Mon Sep 17 00:00:00 2001 From: Xionghaoran <719929980@qq.com> Date: Thu, 7 Nov 2024 16:26:09 +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 | 58 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/function.py b/function.py index d16310e..dba13aa 100644 --- a/function.py +++ b/function.py @@ -1,6 +1,7 @@ # function.py import numpy as np + """ 挑战说明: - 自行编写实现 KNN 分类算法。 @@ -21,7 +22,6 @@ import numpy as np - numpy: 2.1.1 """ - """ 请在这里填写学号和姓名,不填写即成绩无效。 """ @@ -39,36 +39,57 @@ name = "熊浩燃" - int: 预测的分类标签。 """ +def mahalanobis_distance(x1, x2, cov_inv, mean): + """ + 计算两点之间的马氏距离。 + + Args: + x1 (numpy array): 第一个点。 + x2 (numpy array): 第二个点。 + cov_inv (numpy array): 协方差矩阵的逆矩阵。 + mean (numpy array): 数据集的均值。 + + Returns: + float: 两点之间的马氏距离。 + """ + delta = x1 - x2 + dist = delta @ cov_inv @ delta.T + return dist -# 这里写自己的代码实现 def classify0(inX, dataSet, labels, k=3): """ - KNN 分类算法。 + 使用马氏距离的 KNN 分类算法。 Args: - inX (numpy array): 输入待分类的样本。 - dataSet (numpy array): 训练数据集。 - labels (list or numpy array): 训练集对应的标签。 - - k (int): kNN 算法参数,请**自行设置默认值**,评测时仅会使用其默认值,不修改默认值将**无法通过**评测,**成绩为零**。 + - k (int): kNN 算法参数,默认值为 3。 Returns: - int: 预测的分类标签。 """ - # 计算协方差矩阵的逆 + # 计算训练数据集的协方差矩阵和均值 cov_matrix = np.cov(dataSet, rowvar=False) - inv_cov_matrix = np.linalg.inv(cov_matrix) + mean = np.mean(dataSet, axis=0) + cov_inv = np.linalg.inv(cov_matrix + np.eye(cov_matrix.shape[0]) * 1e-5) # 加上一个小的单位矩阵以避免奇异矩阵 - # 计算马氏距离 - diff = dataSet - inX - distances = np.sqrt(np.sum(diff @ inv_cov_matrix * diff, axis=1)) + # 计算待分类样本与训练集中每个样本的马氏距离 + distances = np.array([mahalanobis_distance(inX, x, cov_inv, mean) for x in dataSet]) - # 按距离排序并返回 k 个最近的标签 - sorted_indices = distances.argsort() - nearest_labels = [labels[i] for i in sorted_indices[:k]] + # 获取距离最近的 k 个样本的索引 + sortedDistIndices = np.argpartition(distances, k)[:k] - # 找出出现次数最多的标签 - label_counts = np.bincount(nearest_labels) - return label_counts.argmax() + # 获取这些样本对应的标签 + classCount = {} + for i in sortedDistIndices: + voteLabel = labels[i] + classCount[voteLabel] = classCount.get(voteLabel, 0) + 1 + + # 选择出现次数最多的标签作为预测标签 + sortedClassCount = sorted(classCount.items(), key=lambda x: x[1], reverse=True) + return sortedClassCount[0][0] + def load_data(file_path): """ @@ -82,7 +103,10 @@ def load_data(file_path): if __name__ == "__main__": # 以下代码可用于测试,可自行随意修改 - train_file = "datingTrainSet2.tsv" train_data, train_labels = load_data(train_file) - print(classify0(np.array([65915, 3.868359, 0.128078]), train_data, train_labels)) \ No newline at end of file + + # 测试一个样本 + test_sample = np.array([65915, 3.868359, 0.128078]) + predicted_label = classify0(test_sample, train_data, train_labels) + print(f"The predicted label for the test sample is: {predicted_label}") \ No newline at end of file