一、带交叉验证的学习曲线的代码实操:
尝试在代码框执行以下代码:
(1)导入所需要的模块和库
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn.model_selection import cross_val_score
(2)建立数据集
data = load_breast_cancer()
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=420) # 测试集所占的比例
(3)绘制学习曲线
# 设置两个列表保存生成的评估结果
train_score_list = []
test_score_list = []
score = []
var = []
# 循环不断改变k值, 重新建模 , 重新训练, 然后评估
# k = 1- 20
for k in range(1, 21):
# 实例化
knn = KNeighborsClassifier(n_neighbors=k)
# 采用训练集进行训练
knn.fit(X_train, y_train)
# 评估模型
# 可以评估模型在训练集上的表现情况
train_score_list.append(knn.score(X_train, y_train))
# 评估在测试集上的表现情况
test_score_list.append(knn.score(X_test, y_test))
# 交叉验证平均分数
cross = cross_val_score(knn, X_train, y_train, cv=5)
score.append(cross.mean()) # 每次交叉验证返回的得分数组,再求数组均值
var.append(cross.var())
(4)绘制成折线图
plt.figure(dpi=200)
plt.plot(range(1, 21), train_score_list, label='train_score');
plt.plot(range(1, 21), test_score_list, label='test_score');
plt.plot(range(1, 21), score, color='g',label='cross_score');
plt.plot(range(1, 21),np.array(score)+np.array(var)*2,c='red',linestyle='--')
plt.plot(range(1, 21),np.array(score)-np.array(var)*2,c='red',linestyle='--')
plt.xlabel('k_value')
plt.ylabel('socre')
plt.legend();
(5)最大分数对应的k值
np.argmax(score)+1
(6)最高分数值
max(score)
问题:
尝试输出本节代码运行的:
(1)最大分数
(2)最优k值