LeaveOneGroupOut#
- class sklearn.model_selection.LeaveOneGroupOut[源代码]#
Leave One Group Out cross-validator.
提供训练/测试索引来拆分数据,以便每个训练集由所有样本组成,但属于特定组的样本除外。任意域特定的组信息作为对每个样本的组进行编码的整组信息提供。
例如,这些组可能是样本收集的年份,因此允许针对基于时间的拆分进行交叉验证。
阅读更多的 User Guide .
参见
GroupKFold
具有非重叠组的K折叠迭代器变体。
注意到
拆分根据遗漏组的索引排序。第一次拆分具有测试集,该测试集由其索引在中的组组成
groups
是最低的,等等。示例
>>> import numpy as np >>> from sklearn.model_selection import LeaveOneGroupOut >>> X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) >>> y = np.array([1, 2, 1, 2]) >>> groups = np.array([1, 1, 2, 2]) >>> logo = LeaveOneGroupOut() >>> logo.get_n_splits(X, y, groups) 2 >>> logo.get_n_splits(groups=groups) # 'groups' is always required 2 >>> print(logo) LeaveOneGroupOut() >>> for i, (train_index, test_index) in enumerate(logo.split(X, y, groups)): ... print(f"Fold {i}:") ... print(f" Train: index={train_index}, group={groups[train_index]}") ... print(f" Test: index={test_index}, group={groups[test_index]}") Fold 0: Train: index=[2 3], group=[2 2] Test: index=[0 1], group=[1 1] Fold 1: Train: index=[0 1], group=[1 1] Test: index=[2 3], group=[2 2]
- get_metadata_routing()[源代码]#
获取此对象的元数据路由。
请检查 User Guide 关于路由机制如何工作。
- 返回:
- routingMetadataRequest
A
MetadataRequest
封装路由信息。
- get_n_splits(X=None, y=None, groups=None)[源代码]#
返回交叉验证器中分裂迭代的数量。
- 参数:
- X对象
总是被忽略,存在是为了兼容性。
- y对象
总是被忽略,存在是为了兼容性。
- groups形状类似阵列(n_samples,)
将数据集拆分为训练/测试集时使用的样本标签分组。必须始终指定此“groups”参数以计算拆分数,但可以省略其他参数。
- 返回:
- n_splitsint
返回交叉验证器中分裂迭代的数量。
- set_split_request(*, groups: bool | None | str = '$UNCHANGED$') LeaveOneGroupOut [源代码]#
请求元数据传递给
split
法请注意,此方法仅适用于以下情况
enable_metadata_routing=True
(见sklearn.set_config
).请参阅 User Guide 关于路由机制如何工作。The options for each parameter are:
True
:元数据被请求并传递给split
如果提供的话。如果未提供元数据,则会忽略请求。False
:未请求元数据,元估计器不会将其传递给split
.None
:不请求元数据,如果用户提供元估计器,则元估计器将引发错误。str
:元数据应通过此给定别名而不是原始名称传递给元估计器。
默认 (
sklearn.utils.metadata_routing.UNCHANGED
)保留现有请求。这允许您更改某些参数的请求,而不是其他参数。Added in version 1.3.
备注
只有当该估计器用作元估计器的子估计器时,该方法才相关,例如在
Pipeline
.否则就没有效果了。- 参数:
- groups字符串、真、假或无, 默认=sklearn.utils. metalics_Routing.UNChanged
元数据路由
groups
参数split
.
- 返回:
- self对象
更新的对象。
- split(X, y=None, groups=None)[源代码]#
生成索引将数据拆分为训练集和测试集。
- 参数:
- X形状类似阵列(n_samples,n_features)
训练数据,在哪里
n_samples
是样本数量和n_features
是功能的数量。- y形状类似数组(n_samples,),默认=无
监督学习问题的目标变量。
- groups形状类似阵列(n_samples,)
Group labels for the samples used while splitting the dataset into train/test set.
- 收益率:
- trainndarray
训练为该分裂设置了指数。
- testndarray
测试为该分裂设置了指数。