diff --git a/function.py b/function.py index fb6d2ab..00edba2 100644 --- a/function.py +++ b/function.py @@ -29,7 +29,7 @@ id = "202210423107" name = "刘燚燊" -def load_data(directory): +def load_dataset(directory): """ 读取训练集或测试集的数字图像,转换为矩阵格式。 Args: @@ -80,4 +80,14 @@ def classify(input_vector, dataSet, labels, k=3): # 使用投票机制决定分类 most_common_label = Counter(k_neighbors_labels).most_common(1)[0][0] + return most_common_label + # 获取距离最近的 k 个样本的索引 + k_neighbors_indices = distances.argsort()[:k] + + # 获取这些样本对应的标签 + k_neighbors_labels = labels[k_neighbors_indices] + + # 使用投票机制决定分类 + most_common_label = Counter(k_neighbors_labels).most_common(1)[0][0] + return most_common_label \ No newline at end of file