From 798afa851fad9b3b94536b1b9fbd3cb213870d2d Mon Sep 17 00:00:00 2001 From: wujunjie <297081150@qq.com> Date: Fri, 11 Oct 2024 11:14: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 | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/function.py b/function.py index 2ee7ccf..5b38298 100644 --- a/function.py +++ b/function.py @@ -1,7 +1,5 @@ # function.py -pip install sklearn -import numpy as np -from sklearn.preprocessing import StandardScaler +import numpy as np from numpy.linalg import inv """ 挑战说明: @@ -58,11 +56,7 @@ def classify0(inX, dataSet, labels, k=4, cov_inv=None): if cov_inv is None: # 计算训练数据的协方差矩阵并求逆 cov_matrix = np.cov(dataSet.T) - cov_inv = inv(cov_matrix) - - # 计算待分类样本与所有训练样本之间的马氏距离 - # 注意:这里我们假设 inX 已经被适当地预处理(例如,通过 StandardScaler 归一化) - # 并且 dataSet 也是用相同的预处理步骤处理的 + cov_inv = inv(cov_matrix) differences = dataSet - inX mahalanobis_distances = np.sqrt(np.sum(differences @ cov_inv @ differences.T, axis=1))