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

This commit is contained in:
2024-10-15 15:58:16 +08:00
parent e0a0ca64f1
commit fcde9e1a69
+18 -80
View File
@@ -41,48 +41,25 @@ name = "邓龙树"
# 这里写自己的代码实现
import numpy as np
import os
from collections import Counter
import numpy as np
import os
from collections import Counter
# 这里写自己的代码实现
def classify(input_vector, dataSet, labels, k=1):
"""
使用 KNN 算法对输入样本进行分类。
参数:
- input_vector (numpy array): 输入待分类的样本(1xN)。
- dataSet (numpy array): 训练数据集(MxN)。
- labels (list or numpy array): 训练集对应的标签。
- k (int): kNN 算法参数,默认值为 3。
返回:
- int: 预测的分类标签。
"""
def classify(input_vector, dataSet, labels, k=1): # 设置默认值 k=1
"""使用 KNN 算法对输入样本进行分类。"""
# 计算输入向量与训练集之间的欧氏距离
distances = np.sum((input_vector - dataSet) ** 2, axis=1) ** 0.5
distances = np.linalg.norm(dataSet - input_vector, axis=1)
# 根据距离排序并获取索引
sorted_indices = distances.argsort()
# 选择距离最近的k个点
k_nearest_labels = labels[sorted_indices[:k]].tolist() # 将 NumPy 数组转换为列表
k_nearest_labels = labels[sorted_indices[:k]]
# 统计k个点中各类别的数量
count = Counter(k_nearest_labels)
# 返回数量最多的类别标签
return count.most_common(1)[0][0]
# 这里也写自己的代码实现
def load_dataset(directory):
"""
加载数据集中的图像,并将其转换为特征向量和标签。
参数:
- directory (str): 数据文件夹路径。
返回:
- np.array: 特征数据(Mx1024)。
- np.array: 标签数据(Mx1)。
"""
def load_dataset(directory):
"""加载数据集中的图像,并将其转换为特征向量和标签。"""
# 初始化特征数据列表和标签列表
feature_list = []
label_list = []
@@ -100,24 +77,14 @@ def load_dataset(directory):
feature_matrix = np.array(feature_list)
label_array = np.array(label_list)
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()])
def img_to_vector(filename):
"""将二值图像转换为向量。"""
# 读取文件内容并转换为整数数组
with open(filename, 'r') as file:
lines = file.readlines()
# 将每行的数字字符转换为整数,并使用 NumPy 的 reshape 方法转换为一维数组
vector = np.array([int(char) for line in lines for char in line.strip()]).reshape(1024,)
return vector
if __name__ == "__main__":
@@ -126,36 +93,7 @@ if __name__ == "__main__":
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(