From 8719f435de9f3bd2c5d6685d020c5950b5907a2c Mon Sep 17 00:00:00 2001 From: lzh <202210423221@qq.com> Date: Wed, 16 Oct 2024 23:55:29 +0800 Subject: [PATCH] 222 --- function.py | 90 +++++++++++++++++++++++++---------------------------- 1 file changed, 42 insertions(+), 48 deletions(-) diff --git a/function.py b/function.py index 7757bb6..333e89a 100644 --- a/function.py +++ b/function.py @@ -1,6 +1,5 @@ # function.py import numpy as np -from collections import Counter import os """ @@ -41,10 +40,6 @@ name = "廖子豪" """ -# 这里写自己的代码实现 -import numpy as np -import os -from collections import Counter def img_to_vector(filename): """ @@ -57,9 +52,7 @@ def img_to_vector(filename): - np.array: 二值图像向量。 """ with open(filename, 'r') as file: - lines = file.readlines() - vector = [int(pixel) for line in lines for pixel in line.strip()] - return np.array(vector) + return np.array([int(char) for line in file for char in line.strip()]) def load_dataset(directory): """ @@ -72,7 +65,7 @@ def load_dataset(directory): - np.array: 特征数据。 - np.array: 标签数据。 """ - images = [] + features = [] labels = [] for label_dir in os.listdir(directory): label_dir_path = os.path.join(directory, label_dir) @@ -80,9 +73,9 @@ def load_dataset(directory): for filename in os.listdir(label_dir_path): file_path = os.path.join(label_dir_path, filename) image_vector = img_to_vector(file_path) - images.append(image_vector) + features.append(image_vector) labels.append(int(label_dir)) - return np.array(images), np.array(labels) + return np.array(features), np.array(labels) def classify(input_vector, dataSet, labels, k=3): """ @@ -107,46 +100,47 @@ if __name__ == "__main__": train_dir = "trainingDigits" # 训练集目录 test_dir = "testDigits" # 测试集目录 - train_images, train_labels = load_dataset(train_dir) - test_images, test_labels = load_dataset(test_dir) + train_data, train_labels = load_dataset(train_dir) + test_data, test_labels = load_dataset(test_dir) - print("训练集特征形状:", train_images.shape) + print("训练集特征形状:", train_data.shape) print("训练集标签形状:", train_labels.shape) - print("测试集特征形状:", test_images.shape) + print("测试集特征形状:", test_data.shape) print("测试集标签形状:", test_labels.shape) # 测试 classify 函数 - test_data = np.array([int(char) for char in "00000000000000110000000000000000000" # 示例数据 - "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"] * 4).reshape(1, 1024) - predicted_label = classify(test_data, train_images, train_labels) + test_data_vector = np.array([int(char) for char in ( + "00000000000000110000000000000000" + "00000000000011111000000000000000" + "00000000000011111100000000000000" + "00000000001111111110000000000000" + "00000000001111111110000000000000" + "00000000001111111100000000000000" + "00000000011111111000000000000000" + "00000000111111111000000000000000" + "00000000111111110000000000000000" + "00000000111111110000000000000000" + "00000000111111110000000000000000" + "00000000111111110000000000000000" + "00000000111111100000000000000000" + "00000001111111100000000000000000" + "00000001111111100000000000000000" + "00000001111111100000000000000000" + "00000001111111111111000000000000" + "00000001111111111111111000000000" + "00000011111111111111111100000000" + "00000011111111111111111100000000" + "00000011111111111111111100000000" + "00000001111111111111111110000000" + "00000001111111111111111110000000" + "00000000111111111100111111100000" + "00000000111111111000111111100000" + "00000000011111111000111111110000" + "00000000011111111111111111000000" + "00000000001111111111111111000000" + "00000000000111111111111111000000" + "00000000000001111111111110000000" + "00000000000000000111100000000000" + )]).reshape(1, 1024) + predicted_label = classify(test_data_vector, train_data, train_labels) print(f"预测的标签: {predicted_label}") \ No newline at end of file