Files
Experiment-kNN/function.py
T
zhangchenghong c02a62fa22
Gitea Actions Experiment Test / Experiment-Actions (push) Successful in 11s
更新 function.py
2024-10-20 13:44:26 +08:00

179 lines
5.9 KiB
Python

# function.py
import numpy as np
import os
"""
实验说明:
- 自行编写实现数据加载与 kNN 的手写数字分类算法。
- 函数输入: 待分类的样本 input_vector, 训练数据集 dataSet, 训练集对应的标签 labels。
- 函数返回: 预测的分类标签。
注意事项:
- 在 function.py 中实现 kNN 分类算法。
- 可以自行导入其他库,需要在 requirements.txt 添加依赖,若未在 requirements.txt 中添加使用到的依赖,则无法自动评测,成绩为零。
- 自动评测脚本仅会加载 classify 和 load_data 函数,请勿修改函数名。
- 训练集和测试集需手动下载解压,并放入当前目录下。
测评环境:
- CPU: Intel Xeon(R) CPU E3-1231 v3 @ 3.40GHz
- OS: Ubuntu 22.04.4 LTS
- Python: 3.10.12
- numpy: 2.1.1
"""
"""
请在这里填写学号和姓名,不填写即成绩无效。
"""
id = "202210423215"
name = "章程鸿·"
"""
参数说明:
- input_vector (numpy array): 输入待分类的样本。
- dataSet (numpy array): 训练数据集。
- labels (list or numpy array): 训练集对应的标签。
- k (int): kNN 算法参数,请**自行设置默认值**,评测时仅会使用其默认值,不修改默认值将**无法通过**评测,**成绩为零**。
返回值:
- int: 预测的分类标签。
"""
# 这里写自己的代码实现
import numpy as np
import os
from collections import Counter
# 这里写自己的代码实现
def classify(input_vector, dataSet, labels, k=2):
"""
使用 KNN 算法对输入样本进行分类。
参数:
- input_vector (numpy array): 输入待分类的样本(1xN)。
- dataSet (numpy array): 训练数据集(MxN)。
- labels (list or numpy array): 训练集对应的标签。
- k (int): kNN 算法参数,默认值为 2。
返回:
- int: 预测的分类标签。
"""
# 将欧氏距离改为马氏距离
mean_vector = np.mean(dataSet, axis=0)
cov_matrix = np.cov(dataSet.T)
inv_cov_matrix = np.linalg.inv(cov_matrix)
diff_vector = input_vector - dataSet
mahalanobis_distances = np.sqrt(np.einsum('ij,jk,ik->i', diff_vector, inv_cov_matrix, diff_vector.T))
sorted_indices = mahalanobis_distances.argsort()
k_nearest_distances = mahalanobis_distances[sorted_indices[:k]]
k_nearest_labels = labels[sorted_indices[:k]]
# 计算权重(距离的倒数)
weights = 1 / k_nearest_distances
# 归一化权重
weights /= weights.sum()
# 计算加权投票
weighted_votes = np.zeros((10,))
for label, weight in zip(k_nearest_labels, weights):
weighted_votes[label] += weight
return np.argmax(weighted_votes)
# 这里也写自己的代码实现
def load_dataset(directory):
"""
加载数据集中的图像,并将其转换为特征向量和标签。
参数:
- directory (str): 数据文件夹路径。
返回:
- np.array: 特征数据(Mx1024)。
- np.array: 标签数据(Mx1)。
"""
feature_list = []
label_list = []
for file in os.listdir(directory):
file_path = os.path.join(directory, file)
feature_vector = img_to_vector(file_path)
feature_list.append(feature_vector)
label_list.append(int(file.split('_')[0]))
feature_matrix = np.array(feature_list)
label_array = np.array(label_list)
# 特征缩放(归一化)
min_value = np.min(feature_matrix)
max_value = np.max(feature_matrix)
feature_matrix = (feature_matrix - min_value) / (max_value - min_value)
return feature_matrix, label_array
# 这里还写自己的代码实现
def img_to_vector(filename):
"""
将二值图像转换为向量。
Args:
- filename (str): 图像文件路径。
Returns:
- np.array: 二值图像向量。
"""
# 初始化向量
vector = np.zeros((1024,)) # 创建一个一维数组
# 打开文件并逐行读取
with open(filename) as file:
for i, line in enumerate(file):
# 将每行的数字字符转换为整数并存储到向量中
vector[i*32:i*32+32] = np.array([int(char) for char in line.strip()])
return vector
if __name__ == "__main__":
# 以下代码可用于测试,可自行随意修改
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,
)
print(f"预测的标签: {predicted_label}")