generated from LufsX/Experiment-kNN
This commit is contained in:
+44
-46
@@ -25,8 +25,8 @@ import os
|
||||
"""
|
||||
请在这里填写学号和姓名,不填写即成绩无效。
|
||||
"""
|
||||
id = "202210423xxx"
|
||||
name = "test_name"
|
||||
id = "202210423216"
|
||||
name = "李潼"
|
||||
|
||||
|
||||
"""
|
||||
@@ -39,9 +39,7 @@ name = "test_name"
|
||||
- int: 预测的分类标签。
|
||||
"""
|
||||
|
||||
|
||||
# 这里写自己的代码实现
|
||||
def classify(input_vector, dataSet, labels, k=0):
|
||||
def classify(input_vector, dataSet, labels, k=3):
|
||||
"""
|
||||
KNN 分类算法。
|
||||
|
||||
@@ -49,14 +47,24 @@ def classify(input_vector, dataSet, labels, k=0):
|
||||
- input_vector (numpy array): 输入待分类的样本。
|
||||
- dataSet (numpy array): 训练数据集。
|
||||
- labels (list or numpy array): 训练集对应的标签。
|
||||
- k (int): kNN 算法参数,请**自行设置默认值**,评测时仅会使用其默认值,不修改默认值将**无法通过**评测,**成绩为零**。
|
||||
- k (int): kNN 算法参数,默认值为3。
|
||||
|
||||
Returns:
|
||||
- int: 预测的分类标签。
|
||||
"""
|
||||
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 = sq_distances ** 0.5
|
||||
sorted_dist_indices = np.argsort(distances)
|
||||
class_count = {}
|
||||
for i in range(k):
|
||||
vote_label = labels[sorted_dist_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]
|
||||
|
||||
|
||||
# 这里也写自己的代码实现
|
||||
def load_dataset(directory):
|
||||
"""
|
||||
从文件夹中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。
|
||||
@@ -68,26 +76,20 @@ def load_dataset(directory):
|
||||
- np.array: 特征数据。
|
||||
- np.array: 标签数据。
|
||||
"""
|
||||
|
||||
|
||||
# 这里还写自己的代码实现
|
||||
def img_to_vector(filename):
|
||||
"""
|
||||
将二值图像转换为向量。
|
||||
该部分应为 load_dataset 函数的辅助子函数,可单独在这里实现,也可直接集成至 load_dataset 函数中。
|
||||
|
||||
Args:
|
||||
- filename (str): 图像文件路径。
|
||||
|
||||
Returns:
|
||||
- np.array: 二值图像向量。
|
||||
"""
|
||||
|
||||
training_files = os.listdir(directory)
|
||||
features = []
|
||||
labels = []
|
||||
for file in training_files:
|
||||
label = int(file.split('_')[0])
|
||||
with open(os.path.join(directory, file), 'r') as f:
|
||||
content = f.read().strip()
|
||||
feature = [int(char) for char in content]
|
||||
features.append(feature)
|
||||
labels.append(label)
|
||||
return np.array(features), np.array(labels)
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 以下代码可用于测试,可自行随意修改
|
||||
train_data, train_labels = load_dataset("trainingDigits")
|
||||
|
||||
test_data = (
|
||||
"00000000000000110000000000000000"
|
||||
"00000000000011111000000000000000"
|
||||
@@ -101,31 +103,27 @@ if __name__ == "__main__":
|
||||
"00000000111111110000000000000000"
|
||||
"00000000111111110000000000000000"
|
||||
"00000000111111110000000000000000"
|
||||
"00000000111111100000000000000000"
|
||||
"00000001111111100000000000000000"
|
||||
"00000001111111100000000000000000"
|
||||
"00000001111111100000000000000000"
|
||||
"00000001111111111111000000000000"
|
||||
"00000001111111111111111000000000"
|
||||
"00000001111111111111111100000000"
|
||||
"00000011111111111111111110000000"
|
||||
"00000011111111111111111110000000"
|
||||
"00000001111111111111111110000000"
|
||||
"00000001111111111111111110000000"
|
||||
"00000000111111111100111111100000"
|
||||
"00000000111111111000111111100000"
|
||||
"00000000011111111001111111100000"
|
||||
"00000000011111111111111111000000"
|
||||
"00000000001111111111111111000000"
|
||||
"00000000000111111111111111000000"
|
||||
"00000000000011111111111100000000"
|
||||
"00000000000001111111110000000000"
|
||||
"00000000000000000111100000000000"
|
||||
"00000000111111110000000000000000"
|
||||
"00000001111111110000000000000000"
|
||||
"00000001111111110000000000000000"
|
||||
"00000001111111110000000000000000"
|
||||
"00000001111111111111111111111111"
|
||||
"11111111111111111111111111111111"
|
||||
"11111111111111111111111111111111"
|
||||
"11111111111111111111111111111111"
|
||||
"11111111111111111111111111111111"
|
||||
"11111111111111111111111111111111"
|
||||
"11111111111111111111111111111111"
|
||||
"11111111111111111111111111111111"
|
||||
"1111111111111111111"
|
||||
)
|
||||
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, 64),
|
||||
train_data,
|
||||
train_labels,
|
||||
)
|
||||
print(f"预测的标签: {predicted_label}")
|
||||
|
||||
)
|
||||
|
||||
print(f"预测的标签: {predicted_label}")
|
||||
|
||||
Reference in New Issue
Block a user