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

This commit is contained in:
cy
2024-10-15 20:44:41 +08:00
parent 9caa280c82
commit e002a736e6
+53 -47
View File
@@ -1,6 +1,6 @@
import numpy as np import numpy as np
import os import os
from scipy.spatial import KDTree
""" """
实验说明: 实验说明:
- 自行编写实现数据加载与 kNN 的手写数字分类算法。 - 自行编写实现数据加载与 kNN 的手写数字分类算法。
@@ -28,7 +28,7 @@ id = "202210423117"
name = "程悦" name = "程悦"
def classify(input_vector, dataSet, labels, k=20): def classify(input_vector, dataSet, labels, k=2):
""" """
KNN 分类算法。 KNN 分类算法。
@@ -41,11 +41,17 @@ def classify(input_vector, dataSet, labels, k=20):
Returns: Returns:
- int: 预测的分类标签。 - int: 预测的分类标签。
""" """
tree = KDTree(dataSet) dataSet_size = dataSet.shape[0]
distances, indices = tree.query(input_vector, k=k) # 计算欧氏距离
diff_mat = np.tile(input_vector, (dataSet_size, 1)) - dataSet
sq_diff_mat = diff_mat ** 2
sq_distances = sq_diff_mat.sum(axis=1)
distances = np.sqrt(sq_distances)
# 对距离进行排序
sorted_indices = distances.argsort()
class_count = {} class_count = {}
for i in range(k): for i in range(k):
vote_label = labels[indices[i]] vote_label = labels[sorted_indices[i]]
class_count[vote_label] = class_count.get(vote_label, 0) + 1 class_count[vote_label] = class_count.get(vote_label, 0) + 1
sorted_class_count = sorted(class_count.items(), key=lambda x: x[1], reverse=True) sorted_class_count = sorted(class_count.items(), key=lambda x: x[1], reverse=True)
return sorted_class_count[0][0] return sorted_class_count[0][0]
@@ -94,47 +100,47 @@ def load_dataset(directory):
if __name__ == "__main__": if __name__ == "__main__":
# 以下代码可用于测试,可自行随意修改 # # 以下代码可用于测试,可自行随意修改
train_data, train_labels = load_dataset("trainingDigits") # train_data, train_labels = load_dataset("trainingDigits")
test_data = ( # test_data = (
"00000000000000110000000000000000" # "00000000000000110000000000000000"
"00000000000011111000000000000000" # "00000000000011111000000000000000"
"00000000000011111100000000000000" # "00000000000011111100000000000000"
"00000000001111111110000000000000" # "00000000001111111110000000000000"
"00000000001111111110000000000000" # "00000000001111111110000000000000"
"00000000001111111100000000000000" # "00000000001111111100000000000000"
"00000000011111111000000000000000" # "00000000011111111000000000000000"
"00000000111111111000000000000000" # "00000000111111111000000000000000"
"00000000111111110000000000000000" # "00000000111111110000000000000000"
"00000000111111110000000000000000" # "00000000111111110000000000000000"
"00000000111111110000000000000000" # "00000000111111110000000000000000"
"00000000111111110000000000000000" # "00000000111111110000000000000000"
"00000000111111100000000000000000" # "00000000111111100000000000000000"
"00000001111111100000000000000000" # "00000001111111100000000000000000"
"00000001111111100000000000000000" # "00000001111111100000000000000000"
"00000001111111100000000000000000" # "00000001111111100000000000000000"
"00000001111111111111000000000000" # "00000001111111111111000000000000"
"00000001111111111111111000000000" # "00000001111111111111111000000000"
"00000001111111111111111100000000" # "00000001111111111111111100000000"
"00000011111111111111111110000000" # "00000011111111111111111110000000"
"00000011111111111111111110000000" # "00000011111111111111111110000000"
"00000001111111111111111110000000" # "00000001111111111111111110000000"
"00000001111111111111111110000000" # "00000001111111111111111110000000"
"00000000111111111100111111100000" # "00000000111111111100111111100000"
"00000000111111111000111111100000" # "00000000111111111000111111100000"
"00000000011111111001111111100000" # "00000000011111111001111111100000"
"00000000011111111111111111000000" # "00000000011111111111111111000000"
"00000000001111111111111111000000" # "00000000001111111111111111000000"
"00000000000111111111111111000000" # "00000000000111111111111111000000"
"00000000000011111111111100000000" # "00000000000011111111111100000000"
"00000000000001111111110000000000" # "00000000000001111111110000000000"
"00000000000000000111100000000000" # "00000000000000000111100000000000"
) # )
predicted_label = classify( # predicted_label = classify(
np.array([int(char) for char in test_data]).reshape(1, 1024), # np.array([int(char) for char in test_data]).reshape(1, 1024),
train_data, # train_data,
train_labels, # train_labels,
) # )
print(f"预测的标签: {predicted_label}") # print(f"预测的标签: {predicted_label}")