from sklearn.model_selection import cross_val_score # K折交叉验证模块
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split # 分割数据模块
from sklearn.neighbors import KNeighborsClassifier # K最近邻(kNN,k-NearestNeighbor)分类算法
import matplotlib.pyplot as plt
# 加载iris数据集
iris = load_iris()
x = iris.data
y = iris.target
# #分割数据集
# x_train,x_test,y_train,y_test = train_test_split(x,y,random_state=4)
# 建立模型
knn = KNeighborsClassifier()
# 使用K折交叉验证,准确率(accuracy)用于判断分类(Classification)模型的好坏
# score = cross_val_score(knn, x, y, cv=5, scoring="accuracy")
# 将5次的预测准确率打印出
# print(score)
# 将5次的预测准确平均率打印出
# print(score.mean())
# 建立测试模型参数
k_range = range(1, 31)
k_score = []
# 由迭代的方式来计算不同参数对模型的影响,并返回交叉验证后的平均准确率
for k in k_range:
knn = KNeighborsClassifier(n_neighbors=k)
score = cross_val_score(knn, x, y, cv=10, scoring="accuracy")
k_score.append(score.mean())
# 可视化数据
plt.plot(k_range, k_score)
plt.xlabel("Value of K for KNN")
plt.ylabel("Cross-Validated Accuracy")
plt.show()
交叉验证_分类
最新推荐文章于 2024-05-15 20:55:16 发布