![v2-17d0f7461b2cecbbf0a6589e91ba7546_1440w.jpg?source=172ae18b](http://img-02.proxy.5ce.com/view/image?&type=2&guid=a8eda102-4c2f-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-17d0f7461b2cecbbf0a6589e91ba7546_1440w.jpg?source=172ae18b)
sklearn.model_selection.cross_val_score
(
estimator,
X,
y=None,
*,
groups=None,
scoring=None,
cv=None,
n_jobs=None,
verbose=0,
fit_params=None,
pre_dispatch='2*n_jobs',
error_score=nan)
前面我们提到了4种分割数据集的方法,在分割完数据集后,我们训练模型,那模型的表现到底如何呢?我们可以使用这个函数来看模型的评分。
- estimator:estimator object implementing ‘fit’
我们的算法模型
- X:array-like of shape (n_samples, n_features)
- y:array-like of shape (n_samples,) or (n_samples, n_outputs), default=None
我们的数据集
- cv:int, cross-validation generator or an iterable, default=None
我们要进行的交叉验证的方法
![v2-f1f1378a473404e56f54b71faf0da62c_b.jpg](http://img-01.proxy.5ce.com/view/image?&type=2&guid=a8eda102-4c2f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-f1f1378a473404e56f54b71faf0da62c_b.jpg)
几个常用的参数如上所示,下面,我们举几个小栗子。
- KFold
关于KFold参考:
橘猫吃不胖:sklearn函数:KFold(分割训练集和测试集)zhuanlan.zhihu.com![v2-b9949df6c1a2a5a335ac28d00ccb62a9_180x120.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=a8eda102-4c2f-eb11-8da9-e4434bdf6706&url=https://pic2.zhimg.com/v2-b9949df6c1a2a5a335ac28d00ccb62a9_180x120.jpg)
对于K折交叉验证, K的取值,通常为3、5、10
import pandas as pd
import numpy as np
names = ['preg','plas','pres','skin','test','mass','pedi','age','class']
df = pd.read_csv('data/pima-indians-diabetes.csv' , names=names)
df.head(10)
![v2-86d52dcdffcc8114c18e923b1efb5322_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=a8eda102-4c2f-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-86d52dcdffcc8114c18e923b1efb5322_b.jpg)
X = df.values[:,:8]
y = df.values[:,8]
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression
kfold = KFold(n_splits=10)
model = LogisticRegression()
result = cross_val_score(model , X , y , cv=kfold)
最后的result,就是我们每一次模型的得分(准确率)
这里,我们调用KFold,设置K=10,也就是会分割为10分子集,然后去遍历调用模型
这里,我们使用的是逻辑回归模型
![v2-fbcc5e604f2ae2e49d03176e5c2484ee_b.png](http://img-01.proxy.5ce.com/view/image?&type=2&guid=a8eda102-4c2f-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-fbcc5e604f2ae2e49d03176e5c2484ee_b.png)
2. LeaveOneOut
关于LeaveOneOut,参考:
橘猫吃不胖:sklearn函数:LeaveOneOut(分割训练集和测试集)zhuanlan.zhihu.com![v2-bd0d451b25c5c17624234f8a94f6b86c_180x120.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=a8eda102-4c2f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-bd0d451b25c5c17624234f8a94f6b86c_180x120.jpg)
同样使用上面的数据集
from sklearn.model_selection import LeaveOneOut
loocv = LeaveOneOut()
model = LogisticRegression(max_iter=1000)
result = cross_val_score(model , X , y , cv=loocv)
result
result.mean()
![v2-cc4ba2ce9a151815f20bea77ea8eddc4_b.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=a8eda102-4c2f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-cc4ba2ce9a151815f20bea77ea8eddc4_b.jpg)
这个跑起来的确很慢,一开始结果都是0,1我还以为错了,啥情况,一想,因为留一法,就是遍历N次,每次的结果要么是对的,要么就是不对的,所以,可以看个平均的得分
![v2-bbb99197110a378ab59dbe703a7edae4_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=a8eda102-4c2f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-bbb99197110a378ab59dbe703a7edae4_b.jpg)