generated from LufsX/Experiment-kNN
This commit is contained in:
+53
-47
@@ -1,6 +1,6 @@
|
||||
import numpy as np
|
||||
import os
|
||||
from scipy.spatial import KDTree
|
||||
|
||||
"""
|
||||
实验说明:
|
||||
- 自行编写实现数据加载与 kNN 的手写数字分类算法。
|
||||
@@ -28,7 +28,7 @@ id = "202210423117"
|
||||
name = "程悦"
|
||||
|
||||
|
||||
def classify(input_vector, dataSet, labels, k=20):
|
||||
def classify(input_vector, dataSet, labels, k=2):
|
||||
"""
|
||||
KNN 分类算法。
|
||||
|
||||
@@ -41,11 +41,17 @@ def classify(input_vector, dataSet, labels, k=20):
|
||||
Returns:
|
||||
- int: 预测的分类标签。
|
||||
"""
|
||||
tree = KDTree(dataSet)
|
||||
distances, indices = tree.query(input_vector, k=k)
|
||||
dataSet_size = dataSet.shape[0]
|
||||
# 计算欧氏距离
|
||||
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 = {}
|
||||
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
|
||||
sorted_class_count = sorted(class_count.items(), key=lambda x: x[1], reverse=True)
|
||||
return sorted_class_count[0][0]
|
||||
@@ -94,47 +100,47 @@ def load_dataset(directory):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 以下代码可用于测试,可自行随意修改
|
||||
train_data, train_labels = load_dataset("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,
|
||||
)
|
||||
# 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,
|
||||
# )
|
||||
|
||||
print(f"预测的标签: {predicted_label}")
|
||||
# print(f"预测的标签: {predicted_label}")
|
||||
Reference in New Issue
Block a user