RepeatedStratifiedKFold#

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

重复的按类别分层K折交叉验证器。

对分层K折交叉验证重复n次,每次重复采用不同的随机化。

详情请参阅用户指南

注意

基于类别标签进行分层解决的是工程问题而非统计问题。更多详情请参阅基于类别标签分层的交叉验证迭代器

参数:
n_splits整型,默认为5

折叠(fold)的数量。必须至少为2。

n_repeats整型,默认为10

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

random_state整型、RandomState实例或None,默认为None

控制每次重复的随机状态生成。传入一个整型值可使多次函数调用产生可复现的输出。参见术语表

另请参见

RepeatedKFold

重复K折交叉验证n次。

备注

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

示例

>>> import numpy as np
>>> from sklearn.model_selection import RepeatedStratifiedKFold
>>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])
>>> y = np.array([0, 0, 1, 1])
>>> rskf = RepeatedStratifiedKFold(n_splits=2, n_repeats=2,
...     random_state=36851234)
>>> rskf.get_n_splits(X, y)
4
>>> print(rskf)
RepeatedStratifiedKFold(n_repeats=2, n_splits=2, random_state=36851234)
>>> for i, (train_index, test_index) in enumerate(rskf.split(X, y)):
...     print(f"Fold {i}:")
...     print(f"  Train: index={train_index}")
...     print(f"  Test:  index={test_index}")
...
Fold 0:
  Train: index=[1 2]
  Test:  index=[0 3]
Fold 1:
  Train: index=[0 3]
  Test:  index=[1 2]
Fold 2:
  Train: index=[1 3]
  Test:  index=[0 2]
Fold 3:
  Train: index=[0 2]
  Test:  index=[1 3]
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, groups=None)[source]#

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

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

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

请注意,提供 y 足以生成分割,因此 np.zeros(n_samples) 可用作 X 的占位符,而不是实际训练数据。

y形状类似数组 (n_samples,)

用于监督学习问题的目标变量。分层是基于y标签完成的。

groups对象

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

生成:
trainndarray

该次分割的训练集索引。

testndarray

该次分割的测试集索引。

备注

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