本文章代码实现以下功能:
利用sklearn设计实现k-近邻法。
利用NumPy设计实现k-近邻法。
将设计的k-近邻法对鸢尾花数据集进行分类,通过准确率来验证所设计算法的正确性,并将分类结果可视化。
评估k取不同值时算法的精度,并通过可视化展示。
sklearn实现
# (1)数据导入,分割数据
# 导入iris数据集
from sklearn.datasets import load_iris
# 分割数据模块
from sklearn.model_selection import train_test_split
# (2)K最近(KNN,K-Nearest Neighbor)分类算法
from sklearn.neighbors import KNeighborsClassifier
# 加载iris数据集
data = load_iris()
# 导入数据和标签
data_X = data.data
data_y = data.target
# ———————画图,看第一和第三特征的分布——————————————————
import matplotlib.pyplot as plt
print(data.feature_names)
# print(data.data[:, 0])
# print(data.data[:, 2])
feature_1 = data.data[:, 0] # 设置横坐标标签 代表的是花萼长度
feature_3 = data.data[:, 2] # 设置纵坐标标签 代表的是花瓣宽度
plt.scatter(feature_1, feature_3) # 看数据分布
plt.show()
# _--------------------150个数据的行索引号0-149------------
plt.scatter(feature_1[:50], feature_3[:50], c='red') # 第一类
plt.scatter(feature_1[50:100], feature_3[50:100], c='blueviolet') # 第二类
plt.scatter(feature_1[100:], feature_3[100:], c='darkred') # 第三类
plt.show()
# 分割数据
# 将完整数据集的70%作为训练集,30%作为测试集,
# 并使得测试集和训练集中各类别数据的比例与原始数据集比例一致(stratify分层策略),另外可通过设置shuffle=True 提前打乱数据。
X_train, X_test, y_train, y_test = train_test_split(data_X,
data_y,
random_state=12,
stratify=data_y,
test_size=0.3)
# 建立模型进行训练和预测
# 建立模型
knn = KNeighborsClassifier()
# knn=KNeighborsClassifier(n_neighbors=3)
# (3)训练模型
knn.fit(X_train, y_train)
print(knn.score(X_test, y_test)) # 计算模型的准确率
# (4)预测模型
y_pred = knn.predict(X_test)
print(y_pred - y_test)
# (5)评价— ——用accuracy_score计算准确率— ———————
from sklearn.metrics import accuracy_score
print(accuracy_score(y_test, y_pred)) # 也可以算正确率
print(accuracy_score(y_test, y_pred, normalize=False)) # 统计测试样本分类的个数
# 测试不同的k值
k_range = range(1, 31) # 测试1到30的k值
accuracy = [] # 用于存储每个k值的准确率
for k in k_range:
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
accuracy.append(accuracy_score(y_test, y_pred))
# 绘制k值与准确率的关系图
plt.figure(figsize=(10, 6))
plt.plot(k_range, accuracy, marker='o', linestyle='-', color='b')
plt.title('KNN Varying number of neighbors')
plt.xlabel('Number of neighbors, k')
plt.ylabel('Accuracy')
plt.xticks(k_range)
plt.grid(True)
plt.show()
# (6)保存和加载模型
import joblib
# 用joblib.dump保存模型
joblib.dump(knn, 'iris_KNN.pkl')
# # 用joblib.load加载已保存的模型
knn1 = joblib.load('iris_KNN.pkl')
# #测试读取后的Model
print(knn1.predict(data_X[0:1])) # 预测第一个数据的类别
y_pred1 = knn1.predict(X_test)
print(y_pred1 - y_test)
# 可视化分类结果
plt.figure(figsize=(8, 6)) #设置图形的大小为8英寸宽和6英寸高。
#绘制实际类别
plt.scatter(feature_1[:50], feature_3[:50], c='red', label='Actual Setosa')
plt.scatter(feature_1[50:100], feature_3[50:100], c='blueviolet', label='Actual Versicolor')
plt.scatter(feature_1[100:], feature_3[100:], c='darkred', label='Actual Virginica')
#绘制预测类别
plt.scatter(X_test[y_pred == 0][:, 0], X_test[y_pred == 0][:, 2], c='lightcoral', marker='x', label='Predicted Setosa')
plt.scatter(X_test[y_pred == 1][:, 0], X_test[y_pred == 1][:, 2], c='lightblue', marker='^', label='Predicted Versicolor')
plt.scatter(X_test[y_pred == 2][:, 0], X_test[y_pred == 2][:, 2], c='pink', marker='s', label='Predicted Virginica')
plt.xlabel('Sepal Length')
plt.ylabel('Petal Width')
plt.title('KNN Classification Result on Iris Dataset')
plt.legend()
plt.grid(True)
plt.show()
实现结果
用Numpy实现
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
# 加载iris数据集
data = load_iris()
X = data.data
y = data.target
# 分割数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=12, stratify=y)
# 定义欧氏距离函数
def euclidean_distance(x1, x2):
return np.sqrt(np.sum((x1 - x2) ** 2))
# 实现KNN算法
class KNN:
def fit(self, X, y):
self.X_train = X
self.y_train = y
def predict(self, X, k=3):
y_pred = [self._predict(x, k) for x in X]
return np.array(y_pred)
def _predict(self, x, k):
# 计算x与训练集中每个点的距离
distances = [euclidean_distance(x, x_train) for x_train in self.X_train]
# 获取k个最近邻的索引
k_indices = np.argsort(distances)[:k]
# 获取这些最近邻对应的标签
k_nearest_labels = [self.y_train[i] for i in k_indices]
# 通过多数投票确定预测类别
most_common = np.bincount(k_nearest_labels).argmax()
return most_common
# 实例化KNN
knn = KNN()
# 训练模型
knn.fit(X_train, y_train)
# 进行预测
y_pred = knn.predict(X_test)
# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
# 评估不同k值的准确率
k_values = range(1, 31)
accuracies = []
for k in k_values:
knn = KNN()
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test, k=k)
accuracy = accuracy_score(y_test, y_pred)
accuracies.append(accuracy)
# 可视化k值与准确率的关系
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.plot(k_values, accuracies, marker='o')
plt.xlabel('Number of Neighbors')
plt.ylabel('Accuracy')
plt.title('KNN Varying number of neighbors')
plt.grid(True)
# 可视化分类结果
plt.subplot(1, 2, 2)
plt.scatter(X_test[:, 2], X_test[:, 3], c=y_pred, cmap=plt.cm.Set1, edgecolor='k')
plt.title('KNN Classification Result')
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')
handles, labels = plt.gca().get_legend_handles_labels()
plt.legend(handles, labels, loc='upper left')
plt.grid(True)
plt.tight_layout()
plt.show()
# 找出最高精度和对应的k值
max_accuracy = max(accuracies)
best_k = k_values[accuracies.index(max_accuracy)]
print(f"The best accuracy is {max_accuracy:.2f} with k = {best_k}")