简介
K折交叉验证:将样本切成K份,每次取其中一份做为测试集,剩余的K-1份做为训练集。根据训练训练出模型或者假设函数。 把这个模型放到测试集上,得到分类率。计算k次求得的分类率的平均值,作为该模型或者假设函数的真实分类率。
在sklearn.model_selection 中提供了几种K折交叉验证。
生成样本
>>> from sklearn.datasets import make_classification
>>> data,target=make_classification(n_samples=10)
>>> print(target)
[1 1 0 1 1 0 0 1 0 0]
sklearn.model_selection.KFold
KFold按数据原有的顺序对数据进行分割。可以通过定义shuffle来打乱顺序。
>>> from sklearn.model_selection import KFold
>>> kfold= KFold(n_splits=5,random_state =None)
>>> for train_index,test_index in kfold.split(data,target):
... print("TRAIN:", train_index, "TEST:", test_index)
... print("TRAIN_target:", target[train_index].mean(), "TEST_target:", target[test_index].mean())
TRAIN: [2 3 4 5 6 7 8 9] TEST: [0 1]
TRAIN_target: 0.375 TEST_target: 1.0
TRAIN: [0 1 4 5 6 7 8 9] TEST: [2 3]
TRAIN_target: 0.5 TEST_target: 0.5
TRAIN: [0 1 2 3 6 7 8 9] TEST: [4 5]
TRAIN_target: 0.5 TEST_target: 0.5
TRAIN: [0 1 2 3 4 5