一、通过交叉验证计算得分
model_selection.cross_val_score(estimatoe,X)
1、estimator:实现了fit函数的学习器
2、X:array-like,需要学习的数据,可以是列表或2d数组
3、y:array-like,可选的,默认为None,监督学习中样本特征向量的真实目标值
4、scoring:string,callable or None,可选的,默认为None
一个字符or一个scorer可调用对象或函数,须实现scorer(estimator,X,y)
5、cv:int,交叉验证生成器或者一个迭代器,可选的,默认为None,决定交叉验证划分策略,cv的可选项有以下几种:
(1)None:使用默认的3-fold交叉验证
(2)Integer:指定在(Stratified)kfold中使用的“折”的数量
(3)可以用作交叉验证生成器的一个对象
(4)一个能够产生train/test划分的迭代器对象
对于integer/None类型的输入,如果estimator是一个分类器而且y是对应的类标签,则默认使用StratifiedKFold。在其他情况下,默认使用Kfold。
(5)返回值:scores,浮点数组,shape=(len(list(cv))),每一次交叉验证的得分弄成一个数组。(默认只有三次cv,就有三个得分)
实例:
#绘制svm在digits数据集上的交叉验证曲线
import numpy as np
from sklearn.model_selection import cross_val_score
from sklearn import datasets,svm
digits = datasets.load_digits()
X = digits.data
y = digits.target
svc = svm.SVC(kernel=‘linear’)
C_s = np.logspace(-10,0,10)
print(‘参数列表长度’,len(C_s))
scores = list()
scores_std = list()
for C in C_s:
svc.C = C
this_scores = cross_val_score(svc,X,y,n_jobs=4)
scores.append(np.mean(this_scores))
scores_std.append(np.std(this_scores))
#绘制交叉验证曲线
import matplotlib.pyplot as plt
plt.figure(1,figsize=(4,3))
plt.clf()
plt.semilogx(C_s,scores)
plt.s
SKLearn模型选择之模型验证方法
最新推荐文章于 2024-06-15 21:22:10 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)