LeaveOneOut#
- class sklearn.model_selection.LeaveOneOut[源代码]#
留一出交叉验证器。
提供训练/测试索引以拆分训练/测试集中的数据。每个样本作为测试集(单例)使用一次,而其余样本形成训练集。
注意:
LeaveOneOut()
相当于KFold(n_splits=n)
和LeavePOut(p=1)
哪里n
是样本数量。由于测试集数量较多(与样本数量相同),这种交叉验证方法的成本可能非常高。对于大型数据集,应该倾向于
KFold
,ShuffleSplit
或StratifiedKFold
.阅读更多的 User Guide .
参见
LeaveOneGroupOut
用于根据数据集的显式、特定于领域的分层拆分数据。
GroupKFold
具有非重叠组的K折叠迭代器变体。
示例
>>> import numpy as np >>> from sklearn.model_selection import LeaveOneOut >>> X = np.array([[1, 2], [3, 4]]) >>> y = np.array([1, 2]) >>> loo = LeaveOneOut() >>> loo.get_n_splits(X) 2 >>> print(loo) LeaveOneOut() >>> for i, (train_index, test_index) in enumerate(loo.split(X)): ... print(f"Fold {i}:") ... print(f" Train: index={train_index}") ... print(f" Test: index={test_index}") Fold 0: Train: index=[1] Test: index=[0] Fold 1: Train: index=[0] Test: index=[1]
- get_metadata_routing()[源代码]#
获取此对象的元数据路由。
请检查 User Guide 关于路由机制如何工作。
- 返回:
- routingMetadataRequest
A
MetadataRequest
封装路由信息。