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

This commit is contained in:
2024-10-16 22:20:24 +08:00
parent 27f934527c
commit 00483aba69
+52 -16
View File
@@ -25,8 +25,8 @@ import os
"""
请在这里填写学号和姓名,不填写即成绩无效。
"""
id = "202210423xxx"
name = "test_name"
id = "202210423105"
name = "魏明帅"
"""
@@ -54,8 +54,23 @@ def classify(input_vector, dataSet, labels, k=0):
Returns:
- int: 预测的分类标签。
"""
"""
欧式距离的 kNN 分类算法。
"""
# 计算欧式距离
distances = np.linalg.norm(dataSet - input_vector, axis=1)
# 获取距离最近的 k 个样本的索引
k_indices = np.argsort(distances)[:k]
# 获取最近邻样本的标签
k_nearest_labels = [labels[i] for i in k_indices]
# 找出出现次数最多的标签
most_common = max(set(k_nearest_labels), key=k_nearest_labels.count)
return most_common
# 这里也写自己的代码实现
def load_dataset(directory):
"""
@@ -68,7 +83,24 @@ def load_dataset(directory):
- np.array: 特征数据。
- np.array: 标签数据。
"""
features = []
labels = []
# 遍历文件夹中的所有文件
for filename in os.listdir(directory):
if filename.endswith(".txt"): # 假设文件是 .txt 格式
filepath = os.path.join(directory, filename)
# 假设文件名格式为 "数字_编号.txt"
label = int(filename.split('_')[0])
# 加载文件中的二进制数据并转换为 1D 向量
image_vector = img_to_vector(filepath)
features.append(image_vector)
labels.append(label)
return np.array(features), np.array(labels)
# 这里还写自己的代码实现
def img_to_vector(filename):
@@ -82,7 +114,11 @@ def img_to_vector(filename):
Returns:
- np.array: 二值图像向量。
"""
with open(filename) as file:
lines = file.readlines()
image_vector = np.array([int(char) for line in lines for char in line.strip()])
return image_vector
if __name__ == "__main__":
# 以下代码可用于测试,可自行随意修改
@@ -94,32 +130,32 @@ if __name__ == "__main__":
"00000000000011111100000000000000"
"00000000001111111110000000000000"
"00000000001111111110000000000000"
"00000000001111111100000000000000"
"00000000001111000000000"
"00000000011111111000000000000000"
"00000000111111111000000000000000"
"000000001111111110000000000000"
"00000000111111110000000000000000"
"00000000111111110000000000000000"
"0000000011111111000000000000"
"00000000111111110000000000000000"
"00000000111111110000000000000000"
"00000000111111100000000000000000"
"0000000011111110000000000"
"00000001111111100000000000000000"
"00000001111111100000000000000000"
"00000001111111100000000000000000"
"00000001111111111111000000000000"
"00000001111111111111111000000000"
"0000000111111111000000000"
"00000001111111111111111100000000"
"00000011111111111111111110000000"
"00000011111111111111111110000000"
"00000001111111111111111110000000"
"0000000111111110000000"
"00000001111111111111111110000000"
"00000000111111111100111111100000"
"00000000111111111000111111100000"
"00000000011111111001111111100000"
"00000000011111111111111111000000"
"000000000111111110011100000"
"000000000111111111111000000"
"00000000001111111111111111000000"
"00000000000111111111111111000000"
"00000000000111111111000000"
"00000000000011111111111100000000"
"00000000000001111111110000000000"
"000000001110000000000"
"00000000000000000111100000000000"
)
predicted_label = classify(