generated from LufsX/Experiment-kNN
This commit is contained in:
+46
-63
@@ -38,16 +38,17 @@ name = "焦国波"
|
|||||||
返回值:
|
返回值:
|
||||||
- int: 预测的分类标签。
|
- int: 预测的分类标签。
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
import os
|
|
||||||
import numpy as np
|
|
||||||
from sklearn.model_selection import train_test_split
|
|
||||||
from sklearn.neighbors import KNeighborsClassifier
|
|
||||||
from sklearn.metrics import accuracy_score
|
|
||||||
import time
|
|
||||||
|
|
||||||
def load_data(directory):
|
def load_data(directory):
|
||||||
|
"""
|
||||||
|
加载数据集,将图像数据转换为向量形式,并提取标签。
|
||||||
|
|
||||||
|
参数:
|
||||||
|
- directory (str): 数据集目录路径。
|
||||||
|
|
||||||
|
返回值:
|
||||||
|
- images (numpy array): 图像数据。
|
||||||
|
- labels (list): 标签列表。
|
||||||
|
"""
|
||||||
images = []
|
images = []
|
||||||
labels = []
|
labels = []
|
||||||
for filename in os.listdir(directory):
|
for filename in os.listdir(directory):
|
||||||
@@ -58,65 +59,47 @@ def load_data(directory):
|
|||||||
labels.append(label)
|
labels.append(label)
|
||||||
return np.array(images), np.array(labels)
|
return np.array(images), np.array(labels)
|
||||||
|
|
||||||
|
def classify(input_vector, dataSet, labels, k=3):
|
||||||
|
"""
|
||||||
|
kNN 分类算法实现。
|
||||||
|
|
||||||
|
参数:
|
||||||
|
- input_vector (numpy array): 输入待分类的样本。
|
||||||
|
- dataSet (numpy array): 训练数据集。
|
||||||
|
- labels (list or numpy array): 训练集对应的标签。
|
||||||
|
- k (int): kNN 算法参数,默认值为 3。
|
||||||
|
|
||||||
|
返回值:
|
||||||
|
- int: 预测的分类标签。
|
||||||
|
"""
|
||||||
|
# 计算输入向量与数据集中每个样本的欧式距离
|
||||||
|
distances = np.sqrt(np.sum((dataSet - input_vector) ** 2, axis=1))
|
||||||
|
|
||||||
|
# 获取 k 个最近邻的索引
|
||||||
|
sortedDistIndices = distances.argsort()
|
||||||
|
|
||||||
|
# 获取 k 个最近邻的标签
|
||||||
|
classCount = {}
|
||||||
|
for i in range(k):
|
||||||
|
voteLabel = labels[sortedDistIndices[i]]
|
||||||
|
classCount[voteLabel] = classCount.get(voteLabel, 0) + 1
|
||||||
|
|
||||||
|
# 多数表决,返回出现次数最多的标签
|
||||||
|
sortedClassCount = sorted(classCount.items(), key=lambda x: x[1], reverse=True)
|
||||||
|
return sortedClassCount[0][0]
|
||||||
|
|
||||||
|
# 以下代码用于测试和演示,实际使用时请删除或注释掉
|
||||||
|
if __name__ == "__main__":
|
||||||
# 加载训练集和测试集
|
# 加载训练集和测试集
|
||||||
X_train, y_train = load_data('trainingDigits')
|
X_train, y_train = load_data('trainingDigits')
|
||||||
X_test, y_test = load_data('testDigits')
|
X_test, y_test = load_data('testDigits')
|
||||||
|
|
||||||
# 数据预处理:将图像数据转换为向量形式
|
# 测试分类函数
|
||||||
X_train = X_train.reshape((X_train.shape[0], -1))
|
test_samples = X_test[:5]
|
||||||
X_test = X_test.reshape((X_test.shape[0], -1))
|
for sample in test_samples:
|
||||||
|
predicted_label = classify(sample, X_train, y_train)
|
||||||
|
print(f"Input Vector: {sample}, Predicted Label: {predicted_label}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# 初始化KNN模型
|
|
||||||
knn = KNeighborsClassifier(n_neighbors=3)
|
|
||||||
|
|
||||||
# 训练模型
|
|
||||||
start_time = time.time()
|
|
||||||
knn.fit(X_train, y_train)
|
|
||||||
end_time = time.time()
|
|
||||||
print(f"Training time: {end_time - start_time} seconds")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# 预测测试集
|
|
||||||
start_time = time.time()
|
|
||||||
y_pred = knn.predict(X_test)
|
|
||||||
end_time = time.time()
|
|
||||||
|
|
||||||
# 计算分类精度
|
|
||||||
accuracy = accuracy_score(y_test, y_pred)
|
|
||||||
print(f"Accuracy: {accuracy * 100:.2f}%")
|
|
||||||
print(f"Prediction time: {end_time - start_time} seconds")
|
|
||||||
|
|
||||||
|
|
||||||
from sklearn.model_selection import GridSearchCV
|
|
||||||
|
|
||||||
# 参数网格
|
|
||||||
param_grid = {
|
|
||||||
'n_neighbors': [3, 5, 7, 9],
|
|
||||||
'metric': ['euclidean', 'manhattan'],
|
|
||||||
'algorithm': ['auto', 'ball_tree', 'kd_tree']
|
|
||||||
}
|
|
||||||
|
|
||||||
# 网格搜索
|
|
||||||
grid_search = GridSearchCV(KNeighborsClassifier(), param_grid, cv=5)
|
|
||||||
grid_search.fit(X_train, y_train)
|
|
||||||
|
|
||||||
# 最佳参数
|
|
||||||
print("Best parameters:", grid_search.best_params_)
|
|
||||||
|
|
||||||
# 使用最佳参数重新训练模型
|
|
||||||
best_knn = grid_search.best_estimator_
|
|
||||||
best_knn.fit(X_train, y_train)
|
|
||||||
|
|
||||||
# 重新评估性能
|
|
||||||
start_time = time.time()
|
|
||||||
y_pred = best_knn.predict(X_test)
|
|
||||||
end_time = time.time()
|
|
||||||
accuracy = accuracy_score(y_test, y_pred)
|
|
||||||
print(f"Optimized Accuracy: {accuracy * 100:.2f}%")
|
|
||||||
print(f"Optimized Prediction time: {end_time - start_time} seconds")
|
|
||||||
"00000000000000000111100000000000"
|
"00000000000000000111100000000000"
|
||||||
)
|
)
|
||||||
predicted_label = classify(
|
predicted_label = classify(
|
||||||
|
|||||||
Reference in New Issue
Block a user