RepeatedKFold#

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

重复K折交叉验证器。

重复K折叠n次,每次重复中的随机化不同。

阅读更多的 User Guide .

参数:
n_splitsint,默认=5

折叠次数。必须至少为2。

n_repeatsint,默认值=10

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

random_stateint,RandomState实例或无,默认=无

控制每个重复交叉验证实例的随机性。传递一个int值,以便在多个函数调用中获得可重复的输出。看到 Glossary .

参见

RepeatedStratifiedKFold

重复分层K折叠n次。

注意到

随机CV拆分器可能会为每次拆分调用返回不同的结果。您可以通过设置使结果相同 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()[源代码]#

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

请检查 User Guide 关于路由机制如何工作。

返回:
routingMetadataRequest

A MetadataRequest 封装路由信息。

get_n_splits(X=None, y=None, groups=None)[源代码]#

返回交叉验证器中分裂迭代的数量。

参数:
X对象

总是被忽略,存在是为了兼容性。 np.zeros(n_samples) 可以用作占位符。

y对象

总是被忽略,存在是为了兼容性。 np.zeros(n_samples) 可以用作占位符。

groups形状类似数组(n_samples,),默认=无

Group labels for the samples used while splitting the dataset into train/test set.

返回:
n_splitsint

返回交叉验证器中分裂迭代的数量。

split(X, y=None, groups=None)[源代码]#

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

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

训练数据,在哪里 n_samples 是样本数量和 n_features 是功能的数量。

y形状类似阵列(n_samples,)

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

groups对象

总是被忽略,存在是为了兼容性。

收益率:
trainndarray

训练为该分裂设置了指数。

testndarray

测试为该分裂设置了指数。