generated from LufsX/Experiment-kNN
This commit is contained in:
+9
-2
@@ -5,7 +5,7 @@ from collections import Counter
|
|||||||
|
|
||||||
# 学号和姓名(这部分在实际代码中可能不需要,但为了完整性保留)
|
# 学号和姓名(这部分在实际代码中可能不需要,但为了完整性保留)
|
||||||
id = "202210423111"
|
id = "202210423111"
|
||||||
name = "李劲"
|
name = ""
|
||||||
|
|
||||||
def classify(input_vector, dataSet, labels, mean_vector, inv_cov_matrix, k=1):
|
def classify(input_vector, dataSet, labels, mean_vector, inv_cov_matrix, k=1):
|
||||||
"""
|
"""
|
||||||
@@ -56,7 +56,14 @@ def load_dataset(directory):
|
|||||||
|
|
||||||
features = np.array(features)
|
features = np.array(features)
|
||||||
labels = np.array(labels)
|
labels = np.array(labels)
|
||||||
|
|
||||||
|
# 假设cov_matrix是原始协方差矩阵
|
||||||
|
|
||||||
|
regularization_term = 1e-5 * np.eye(cov_matrix.shape[0])
|
||||||
|
|
||||||
|
cov_matrix_regularized = cov_matrix + regularization_term
|
||||||
|
|
||||||
|
inv_cov_matrix = np.linalg.inv(cov_matrix_regularized)
|
||||||
# 计算均值向量和协方差矩阵
|
# 计算均值向量和协方差矩阵
|
||||||
mean_vector = np.mean(features, axis=0)
|
mean_vector = np.mean(features, axis=0)
|
||||||
cov_matrix = np.cov(features, rowvar=False)
|
cov_matrix = np.cov(features, rowvar=False)
|
||||||
|
|||||||
Reference in New Issue
Block a user