RepeatedKFold#
- class sklearn.model_selection.RepeatedKFold(*, n_splits=5, n_repeats=10, random_state=None)[source]#
重复 K-Fold 交叉验证器。
以不同的随机化重复 K-折交叉验证
n_repeats次。在用户指南中阅读更多内容。
- 参数:
- n_splitsint, default=5
折叠数。必须至少为 2。
- n_repeatsint, 默认=10
交叉验证器需要重复的次数。
- random_stateint, RandomState instance or None, default=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() 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]#
返回在实例化交叉验证器时使用
n_splits参数设置的拆分迭代次数。- 参数:
- Xarray-like of shape (n_samples, n_features), default=None
始终忽略,存在是为了保持 API 兼容性。
- yshape 为 (n_samples,), default=None 的 array-like
始终忽略,存在是为了保持 API 兼容性。
- groups形状为 (n_samples,) 的类数组对象,默认=None
始终忽略,存在是为了保持 API 兼容性。
- 返回:
- n_splitsint
返回交叉验证器中的拆分迭代次数。
- split(X, y=None, groups=None)[source]#
生成索引以将数据拆分为训练集和测试集。
- 参数:
- Xshape 为 (n_samples, n_features) 的 array-like
训练数据,其中
n_samples是样本数,n_features是特征数。- yshape 为 (n_samples,), default=None 的 array-like
用于监督学习问题的目标变量。
- groups形状为 (n_samples,) 的类数组对象,默认=None
始终忽略,存在是为了保持 API 兼容性。
- 生成:
- trainndarray
该拆分的训练集索引。
- testndarray
该拆分的测试集索引。