generated from LufsX/Experiment-kNN
This commit is contained in:
+2
-34
@@ -22,14 +22,6 @@ def load_dataset(directory):
|
|||||||
|
|
||||||
return np.array(data_matrix), np.array(labels)
|
return np.array(data_matrix), np.array(labels)
|
||||||
|
|
||||||
"""
|
|
||||||
函数名称: img_to_vector
|
|
||||||
功能: 将32x32的二值图像转换为1x1024的向量。
|
|
||||||
参数:
|
|
||||||
- file_path (str): 图像文件的路径。
|
|
||||||
返回值:
|
|
||||||
- vector (numpy array): 转换后的1x1024的向量。
|
|
||||||
"""
|
|
||||||
def img_to_vector(file_path):
|
def img_to_vector(file_path):
|
||||||
vector = np.zeros((1, 1024))
|
vector = np.zeros((1, 1024))
|
||||||
with open(file_path, 'r') as f:
|
with open(file_path, 'r') as f:
|
||||||
@@ -39,18 +31,7 @@ def img_to_vector(file_path):
|
|||||||
vector[0, 32 * i + j] = int(lines[i][j])
|
vector[0, 32 * i + j] = int(lines[i][j])
|
||||||
return vector
|
return vector
|
||||||
|
|
||||||
"""
|
def classify(input_vector, dataSet, labels, k=7):
|
||||||
函数名称: classify
|
|
||||||
功能: 使用kNN算法对输入样本进行分类。
|
|
||||||
参数:
|
|
||||||
- input_vector (numpy array): 输入待分类的样本,shape 为 (1024,)。
|
|
||||||
- dataSet (numpy array): 训练数据集,shape 为 (m, 1024)。
|
|
||||||
- labels (list or numpy array): 训练集对应的标签,shape 为 (m,)。
|
|
||||||
- k (int): kNN 算法的参数,默认值为3。
|
|
||||||
返回值:
|
|
||||||
- int: 预测的分类标签。
|
|
||||||
"""
|
|
||||||
def classify(input_vector, dataSet, labels, k=3):
|
|
||||||
# 计算欧氏距离
|
# 计算欧氏距离
|
||||||
distances = np.linalg.norm(dataSet - input_vector, axis=1)
|
distances = np.linalg.norm(dataSet - input_vector, axis=1)
|
||||||
# 获取距离最近的k个样本的标签
|
# 获取距离最近的k个样本的标签
|
||||||
@@ -64,14 +45,6 @@ def classify(input_vector, dataSet, labels, k=3):
|
|||||||
most_common = Counter(nearest_labels).most_common(1)
|
most_common = Counter(nearest_labels).most_common(1)
|
||||||
return most_common[0][0]
|
return most_common[0][0]
|
||||||
|
|
||||||
"""
|
|
||||||
函数名称: normalize_data
|
|
||||||
功能: 对数据进行归一化处理,将特征缩放到相同范围,通常是 [0, 1]。
|
|
||||||
参数:
|
|
||||||
- dataSet (numpy array): 需要归一化的训练数据集或测试数据集。
|
|
||||||
返回值:
|
|
||||||
- normalized_dataSet (numpy array): 归一化后的数据集。
|
|
||||||
"""
|
|
||||||
def normalize_data(dataSet):
|
def normalize_data(dataSet):
|
||||||
# 将数据集 reshape 成二维数组
|
# 将数据集 reshape 成二维数组
|
||||||
num_samples = dataSet.shape[0]
|
num_samples = dataSet.shape[0]
|
||||||
@@ -81,11 +54,6 @@ def normalize_data(dataSet):
|
|||||||
scaler = MinMaxScaler()
|
scaler = MinMaxScaler()
|
||||||
return scaler.fit_transform(dataSet_reshaped)
|
return scaler.fit_transform(dataSet_reshaped)
|
||||||
|
|
||||||
"""
|
|
||||||
实验主程序:
|
|
||||||
- 加载训练集和测试集。
|
|
||||||
- 测试 kNN 算法的分类效果,并计算分类精度。
|
|
||||||
"""
|
|
||||||
def main():
|
def main():
|
||||||
# 加载训练集和测试集
|
# 加载训练集和测试集
|
||||||
training_data, training_labels = load_dataset('trainingDigits')
|
training_data, training_labels = load_dataset('trainingDigits')
|
||||||
@@ -101,7 +69,7 @@ def main():
|
|||||||
|
|
||||||
for i in range(total_tests):
|
for i in range(total_tests):
|
||||||
test_sample = test_data[i]
|
test_sample = test_data[i]
|
||||||
prediction = classify(test_sample, training_data, training_labels, k=3)
|
prediction = classify(test_sample, training_data, training_labels, k=7)
|
||||||
actual_label = test_labels[i]
|
actual_label = test_labels[i]
|
||||||
|
|
||||||
print(f"测试样本 {i+1}: 预测值 = {prediction}, 实际值 = {actual_label}")
|
print(f"测试样本 {i+1}: 预测值 = {prediction}, 实际值 = {actual_label}")
|
||||||
|
|||||||
Reference in New Issue
Block a user