LeavePOut#

class sklearn.model_selection.LeavePOut(p)[source]#

Leave-P-Out 交叉验证器。

提供训练/测试索引,用于将数据拆分为训练/测试集。这会在每次迭代中对所有大小为 p 的不同样本进行测试,而剩余的 n - p 个样本则构成训练集。

注意:LeavePOut(p) 与创建非重叠测试集的 KFold(n_splits=n_samples // p) 不等价。

由于迭代次数随着样本数量的增加呈组合式增长,这种交叉验证方法可能非常耗时。对于大型数据集,应优先选择 KFoldStratifiedKFoldShuffleSplit

详情请参阅用户指南

参数:
pint

测试集的大小。必须严格小于样本数量。

示例

>>> import numpy as np
>>> from sklearn.model_selection import LeavePOut
>>> X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
>>> y = np.array([1, 2, 3, 4])
>>> lpo = LeavePOut(2)
>>> lpo.get_n_splits(X)
6
>>> print(lpo)
LeavePOut(p=2)
>>> for i, (train_index, test_index) in enumerate(lpo.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=[1 3]
  Test:  index=[0 2]
Fold 2:
  Train: index=[1 2]
  Test:  index=[0 3]
Fold 3:
  Train: index=[0 3]
  Test:  index=[1 2]
Fold 4:
  Train: index=[0 2]
  Test:  index=[1 3]
Fold 5:
  Train: index=[0 1]
  Test:  index=[2 3]
get_metadata_routing()[source]#

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

请查看用户指南了解路由机制的工作原理。

返回:
routingMetadataRequest

一个封装路由信息的 MetadataRequest

get_n_splits(X, y=None, groups=None)[source]#

返回交叉验证器中的拆分迭代次数。

参数:
Xarray-like of shape (n_samples, n_features)

训练数据,其中 n_samples 是样本数量,n_features 是特征数量。

yobject

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

groupsobject

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

split(X, y=None, groups=None)[source]#

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

参数:
Xarray-like of shape (n_samples, n_features)

训练数据,其中 n_samples 是样本数量,n_features 是特征数量。

yarray-like of shape (n_samples,)

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

groupsobject

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

生成:
trainndarray

该拆分的训练集索引。

testndarray

该拆分的测试集索引。