KFold#
- class sklearn.model_selection.KFold(n_splits=5, *, shuffle=False, random_state=None)[source]#
- K 折交叉验证器。 - 提供训练/测试索引以将数据拆分为训练/测试集。将数据集拆分为 k 个连续的折叠(默认情况下不进行混洗)。 - 然后每个折叠都用作一次验证,而 k - 1 个剩余的折叠构成训练集。 - 在 用户指南 中了解更多信息。 - 有关交叉验证行为的可视化以及常用 scikit-learn 分割方法之间的比较,请参考 scikit-learn 中交叉验证行为的可视化 - 参数:
- n_splitsint, default=5
- 折叠数。必须至少为 2。 - 0.22 版本中的更改: - n_splits的默认值已从 3 更改为 5。
- shufflebool, default=False
- 是否在将数据分成批次之前打乱数据。请注意,每个分割中的样本不会被打乱。 
- random_stateint、RandomState 实例或 None,默认为 None
- 当 - shuffle为 True 时,- random_state会影响索引的顺序,从而控制每一折的随机性。否则,此参数无效。为在多次函数调用中获得可重复的结果,请传递一个整数。参见词汇表。
 
 - 另请参见 - StratifiedKFold
- 考虑类信息以避免构建具有不平衡类分布的折(对于二元或多类分类任务)。 
- GroupKFold
- 具有非重叠组的 K 折迭代器变体。 
- RepeatedKFold
- 重复 K 折 n 次。 
 - 注释 - 前 - n_samples % n_splits折的大小为- n_samples // n_splits + 1,其他折的大小为- n_samples // n_splits,其中- n_samples是样本数。- 随机化 CV 分割器每次调用 split 可能会返回不同的结果。您可以通过将 - random_state设置为整数来使结果相同。- 示例 - >>> import numpy as np >>> from sklearn.model_selection import KFold >>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]]) >>> y = np.array([1, 2, 3, 4]) >>> kf = KFold(n_splits=2) >>> kf.get_n_splits(X) 2 >>> print(kf) KFold(n_splits=2, random_state=None, shuffle=False) >>> for i, (train_index, test_index) in enumerate(kf.split(X)): ... print(f"Fold {i}:") ... print(f" Train: index={train_index}") ... print(f" Test: index={test_index}") Fold 0: Train: index=[2 3] Test: index=[0 1] Fold 1: Train: index=[0 1] Test: index=[2 3] - get_metadata_routing()[源代码]#
- 获取此对象的元数据路由。 - 请查看用户指南,了解路由机制的工作原理。 - 返回:
- routingMetadataRequest
- 一个 - MetadataRequest封装路由信息。
 
 
 
 
     
 
 
 
 
