更新 function.py
Gitea Actions Experiment Test / Experiment-Actions (push) Successful in 22s

This commit is contained in:
lzh
2024-10-17 00:18:17 +08:00
parent 2d7303fb10
commit 7c359f4a6b
+85 -19
View File
@@ -41,38 +41,101 @@ name = "廖子豪"
# 这里写自己的代码实现
import numpy as np
import os
def classify(input_vector, dataSet, labels, k=3):
"""
KNN 分类算法。
def classify(input_vector, dataSet, labels, k_values=[1, 31]):
# ... 你的代码 ...
Args:
- input_vector (numpy array): 输入待分类的样本。
- dataSet (numpy array): 训练数据集。
- labels (list or numpy array): 训练集对应的标签。
- k (int): kNN 算法参数,默认值为3。
Returns:
- int: 预测的分类标签。
"""
distances = np.sqrt(np.sum((dataSet - input_vector) ** 2, axis=1))
sorted_indices = np.argsort(distances)
class_counts = {}
for i in range(k):
vote_label = labels[sorted_indices[i]]
class_counts[vote_label] = class_counts.get(vote_label, 0) + 1
sorted_class_counts = sorted(class_counts.items(), key=lambda x: x[1], reverse=True)
return sorted_class_counts[0][0]
def load_dataset(directory):
"""
从文件夹中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。
Args:
- directory (str): 数据文件夹路径。
Returns:
- np.array: 特征数据。
- np.array: 标签数据。
"""
labels = []
dataset = []
file_list = os.listdir(directory)
num_samples = len(file_list)
dataset = np.zeros((num_samples, 1024))
for i, filename in enumerate(file_list):
label = int(filename.split("_")[0]) # 从文件名中提取标签
labels.append(label)
dataset[i, :] = img_to_vector(os.path.join(directory, filename))
return dataset, np.array(labels)
for file_name in file_list:
file_path = os.path.join(directory, file_name)
labels.append(int(file_name.split('_')[0]))
dataset.append(img_to_vector(file_path))
return np.array(dataset), np.array(labels)
def img_to_vector(filename):
with open(filename, 'r') as file_in:
img_vector = np.zeros((1, 1024))
for idx, line in enumerate(file_in):
for col_idx, char in enumerate(line.strip()):
img_vector[0, idx * 32 + col_idx] = int(char)
return img_vector
"""
将二值图像转换为向量。
Args:
- filename (str): 图像文件路径。
Returns:
- np.array: 二值图像向量。
"""
with open(filename, 'r') as file:
return np.array([int(bit) for line in file for bit in line.strip()])
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),
@@ -80,4 +143,7 @@ if __name__ == "__main__":
train_labels,
)
print(f"预测的标签: {predicted_label}")
print(f"预测的标签: {predicted_label}")