RepeatedKFold#

class sklearn.model_selection.RepeatedKFold(*, n_splits=5, n_repeats=10, random_state=None)[source]#

重复K折交叉验证器。

在每次重复中,将K折交叉验证重复 n_repeats 次,每次使用不同的随机化。

详情请参阅用户指南

参数:
n_splits整型, 默认值=5

折叠数。必须至少为2。

n_repeats整型, 默认值=10

交叉验证器需要重复的次数。

random_state整型, RandomState 实例或 None, 默认值=None

控制每个重复交叉验证实例的随机性。传入一个整型值以在多次函数调用中获得可重现的输出。请参阅术语表

另请参阅

RepeatedStratifiedKFold

重复分层K折交叉验证n次。

备注

随机化的交叉验证(CV)分割器每次调用 `split` 可能会返回不同的结果。通过将 random_state 设置为整数,可以使结果相同。

示例

>>> import numpy as np
>>> from sklearn.model_selection import RepeatedKFold
>>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])
>>> y = np.array([0, 0, 1, 1])
>>> rkf = RepeatedKFold(n_splits=2, n_repeats=2, random_state=2652124)
>>> rkf.get_n_splits(X, y)
4
>>> print(rkf)
RepeatedKFold(n_repeats=2, n_splits=2, random_state=2652124)
>>> for i, (train_index, test_index) in enumerate(rkf.split(X)):
...     print(f"Fold {i}:")
...     print(f"  Train: index={train_index}")
...     print(f"  Test:  index={test_index}")
...
Fold 0:
  Train: index=[0 1]
  Test:  index=[2 3]
Fold 1:
  Train: index=[2 3]
  Test:  index=[0 1]
Fold 2:
  Train: index=[1 2]
  Test:  index=[0 3]
Fold 3:
  Train: index=[0 3]
  Test:  index=[1 2]
get_metadata_routing()[source]#

获取此对象的元数据路由。

请查看用户指南以了解路由机制的工作原理。

返回:
routingMetadataRequest

一个封装了路由信息的 MetadataRequest

get_n_splits(X=None, y=None, groups=None)[source]#

返回交叉验证器中的分割迭代次数。

参数:
X对象

始终被忽略,仅为兼容性而存在。np.zeros(n_samples) 可用作占位符。

y对象

始终被忽略,仅为兼容性而存在。np.zeros(n_samples) 可用作占位符。

groups形状为 (n_samples,) 的类数组, 默认值=None

用于将数据集分割成训练/测试集时所使用的样本的组标签。

返回:
n_splits整型

返回交叉验证器中的分割迭代次数。

split(X, y=None, groups=None)[source]#

生成将数据分割为训练集和测试集的索引。

参数:
X形状为 (n_samples, n_features) 的类数组

训练数据,其中 n_samples 是样本数量,n_features 是特征数量。

y形状为 (n_samples,) 的类数组

用于监督学习问题的目标变量。

groups对象

始终被忽略,仅为兼容性而存在。

生成:
trainndarray

该分割的训练集索引。

testndarray

该分割的测试集索引。