generated from LufsX/Experiment-kNN
This commit is contained in:
+20
-7
@@ -60,29 +60,38 @@ def classify(input_vector, dataSet, labels, k=3): # 设置默认值 k=3
|
|||||||
|
|
||||||
def load_dataset(directory):
|
def load_dataset(directory):
|
||||||
"""加载数据集中的图像,并将其转换为特征向量和标签。"""
|
"""加载数据集中的图像,并将其转换为特征向量和标签。"""
|
||||||
|
# 初始化特征数据列表和标签列表
|
||||||
feature_list = []
|
feature_list = []
|
||||||
label_list = []
|
label_list = []
|
||||||
|
# 遍历目录中的所有文件
|
||||||
for file in os.listdir(directory):
|
for file in os.listdir(directory):
|
||||||
|
# 获取文件的完整路径
|
||||||
file_path = os.path.join(directory, file)
|
file_path = os.path.join(directory, file)
|
||||||
|
# 将图像文件转换为向量
|
||||||
feature_vector = img_to_vector(file_path)
|
feature_vector = img_to_vector(file_path)
|
||||||
|
# 将特征向量添加到列表中
|
||||||
feature_list.append(feature_vector)
|
feature_list.append(feature_vector)
|
||||||
|
# 获取文件名中的标签并添加到标签列表中
|
||||||
label_list.append(int(file.split('_')[0]))
|
label_list.append(int(file.split('_')[0]))
|
||||||
|
# 将特征列表和标签列表转换为numpy数组
|
||||||
feature_matrix = np.array(feature_list)
|
feature_matrix = np.array(feature_list)
|
||||||
label_array = np.array(label_list)
|
label_array = np.array(label_list)
|
||||||
return feature_matrix, label_array
|
return feature_matrix, label_array
|
||||||
|
|
||||||
def img_to_vector(filename):
|
def img_to_vector(filename):
|
||||||
"""将二值图像转换为向量。"""
|
"""将二值图像转换为向量。"""
|
||||||
|
# 读取文件内容并转换为整数数组
|
||||||
with open(filename, 'r') as file:
|
with open(filename, 'r') as file:
|
||||||
lines = file.read().strip()
|
lines = file.readlines()
|
||||||
vector = np.array(list(map(int, lines))).reshape(1024,)
|
# 将每行的数字字符转换为整数,并使用 NumPy 的 reshape 方法转换为一维数组
|
||||||
|
vector = np.array([int(char) for line in lines for char in line.strip()]).reshape(1024,)
|
||||||
return vector
|
return vector
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# 测试代码
|
# 以下代码可用于测试,可自行随意修改
|
||||||
train_data, train_labels = load_dataset("trainingDigits")
|
train_data, train_labels = load_dataset("trainingDigits")
|
||||||
|
|
||||||
test_data_str = (
|
test_data = (
|
||||||
"00000000000000110000000000000000"
|
"00000000000000110000000000000000"
|
||||||
"00000000000011111000000000000000"
|
"00000000000011111000000000000000"
|
||||||
"00000000000011111100000000000000"
|
"00000000000011111100000000000000"
|
||||||
@@ -116,6 +125,10 @@ if __name__ == "__main__":
|
|||||||
"00000000000001111111110000000000"
|
"00000000000001111111110000000000"
|
||||||
"00000000000000000111100000000000"
|
"00000000000000000111100000000000"
|
||||||
)
|
)
|
||||||
test_data = np.array(list(map(int, test_data_str))).reshape(1, 1024)
|
predicted_label = classify(
|
||||||
predicted_label = classify(test_data, train_data, train_labels)
|
np.array([int(char) for char in test_data]).reshape(1, 1024),
|
||||||
print(f"预测的标签: {predicted_label}")
|
train_data,
|
||||||
|
train_labels,
|
||||||
|
)
|
||||||
|
|
||||||
|
print(f"预测的标签: {predicted_label}")
|
||||||
Reference in New Issue
Block a user