Files
Experiment/function.py
T
zhe ea9e28dac6
Gitea Actions Experiment Test / Experiment-Actions (push) Has been cancelled
更新 function.py
2024-10-23 11:46:52 +08:00

83 lines
2.6 KiB
Python

import numpy as np
import os
from scipy.spatial.distance import mahalanobis
from collections import Counter
# 学号和姓名
id = "202210423111"
name = ""
def classify(input_vector, dataSet, labels, k=1):
"""
KNN 分类算法,使用马氏距离。
"""
# 计算协方差矩阵及其逆
cov_matrix = np.cov(dataSet.T)
if np.linalg.det(cov_matrix) == 0: # 检查奇异矩阵
cov_matrix += np.eye(cov_matrix.shape[0]) * 1e-8 # 添加正则化
inv_cov_matrix = np.linalg.inv(cov_matrix)
# 计算马氏距离
distances = [mahalanobis(input_vector, point, inv_cov_matrix) for point in dataSet]
# 获取距离最近的 k 个样本的索引
k_indices = np.argsort(distances)[:k]
# 获取最近邻样本的标签
k_nearest_labels = [labels[i] for i in k_indices]
# 找出出现次数最多的标签
most_common = Counter(k_nearest_labels).most_common(1)[0][0]
return most_common
def load_dataset(directory):
"""
从文件夹中加载数据。
"""
features = []
labels = []
# 遍历文件夹中的所有文件
for filename in os.listdir(directory):
if filename.endswith(".txt"): # 假设文件是 .txt 格式
filepath = os.path.join(directory, filename)
try:
label = int(filename.split('_')[0])
image_vector = img_to_vector(filepath)
features.append(image_vector)
labels.append(label)
except Exception as e:
print(f"Error processing file {filepath}: {e}")
return np.array(features), np.array(labels)
def img_to_vector(filename):
"""
将二值图像转换为向量。
"""
try:
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
except Exception as e:
print(f"Error reading file {filename}: {e}")
return np.zeros(1024) # 假设有一个预期的向量长度,此处设为1024
if __name__ == "__main__":
# 测试
train_data, train_labels = load_dataset("trainingDigits")
test_data = (
"00000000000000110000000000000000"
# ... (省略的测试数据)
)
predicted_label = classify(
np.array([int(char) for char in test_data]).reshape(1, 1024),
train_data,
train_labels,
)
print(f"预测的标签: {predicted_label}")