diff --git a/function.py b/function.py index b1f8c16..3275424 100644 --- a/function.py +++ b/function.py @@ -1,4 +1,3 @@ -# function.py import numpy as np import os @@ -21,12 +20,11 @@ import os - numpy: 2.1.1 """ - """ 请在这里填写学号和姓名,不填写即成绩无效。 """ -id = "202210423xxx" -name = "test_name" +id = "202210423228" +name = "黎世浩" """ @@ -41,7 +39,7 @@ name = "test_name" # 这里写自己的代码实现 -def classify(input_vector, dataSet, labels, k=0): +def classify(input_vector, dataSet, labels, k=3): """ KNN 分类算法。 @@ -54,6 +52,25 @@ def classify(input_vector, dataSet, labels, k=0): Returns: - int: 预测的分类标签。 """ + # 计算输入向量与数据集中每个样本的欧氏距离 + distances = [np.linalg.norm(row-input_vector) for row in dataSet] + + # 获取距离最近的k个样本的索引 + k_indices = np.argsort(distances)[:k] + + # 获取这些样本对应的标签 + k_nearest_labels = [labels[i] for i in k_indices] + + # 统计每个标签出现的次数 + label_counts = {} + for label in k_nearest_labels: + if label not in label_counts: + label_counts[label] = 0 + label_counts[label] += 1 + + # 找到出现次数最多的标签 + sorted_labels = sorted(label_counts.items(), key=lambda x: x[1], reverse=True) + return sorted_labels[0][0] # 这里也写自己的代码实现 @@ -68,6 +85,15 @@ def load_dataset(directory): - np.array: 特征数据。 - np.array: 标签数据。 """ + dataset = [] + labels = [] + for filename in os.listdir(directory): + filepath = os.path.join(directory, filename) + vector = img_to_vector(filepath) + label = int(filename.split('_')[0]) + dataset.append(vector) + labels.append(label) + return np.array(dataset), np.array(labels) # 这里还写自己的代码实现 @@ -82,50 +108,26 @@ def img_to_vector(filename): Returns: - np.array: 二值图像向量。 """ + with open(filename, 'r') as file: + # 读取文件内容并忽略换行符 + return np.array([int(char) for char in file.read() if char.isdigit()]) if __name__ == "__main__": - # 以下代码可用于测试,可自行随意修改 + # 确保当前目录下有trainingDigits文件夹 train_data, train_labels = load_dataset("trainingDigits") - test_data = ( - "00000000000000110000000000000000" - "00000000000011111000000000000000" - "00000000000011111100000000000000" - "00000000001111111110000000000000" - "00000000001111111110000000000000" - "00000000001111111100000000000000" - "00000000011111111000000000000000" - "00000000111111111000000000000000" - "00000000111111110000000000000000" - "00000000111111110000000000000000" - "00000000111111110000000000000000" - "00000000111111110000000000000000" - "00000000111111100000000000000000" - "00000001111111100000000000000000" - "00000001111111100000000000000000" - "00000001111111100000000000000000" - "00000001111111111111000000000000" - "00000001111111111111111000000000" - "00000001111111111111111100000000" - "00000011111111111111111110000000" - "00000011111111111111111110000000" - "00000001111111111111111110000000" - "00000001111111111111111110000000" - "00000000111111111100111111100000" - "00000000111111111000111111100000" - "00000000011111111001111111100000" - "00000000011111111111111111000000" - "00000000001111111111111111000000" - "00000000000111111111111111000000" - "00000000000011111111111100000000" - "00000000000001111111110000000000" - "00000000000000000111100000000000" - ) - predicted_label = classify( - np.array([int(char) for char in test_data]).reshape(1, 1024), - train_data, - train_labels, - ) + # 生成测试数据,确保它是1024维的 + test_data = "" + for _ in range(32): # 每行32个字符,共32行 + for _ in range(32): # 每行32个字符 + test_data += str(np.random.randint(0, 2)) # 随机生成0或1 - print(f"预测的标签: {predicted_label}") + # 将测试数据转换为numpy数组并重塑为(1, 1024)的形状 + test_vector = np.array([int(char) for char in test_data]).reshape(1, 1024) + + # 使用kNN算法进行分类 + predicted_label = classify(test_vector, train_data, train_labels) + + # 输出预测的标签 + print(f"预测的标签: {predicted_label}") \ No newline at end of file