LeavePOut#

class sklearn.model_selection.LeavePOut(p)[源代码]#

Leave-P-Out交叉验证器。

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

注意: LeavePOut(p) 不等同于 KFold(n_splits=n_samples // p) 这会创建不重叠的测试集。

由于迭代次数较多,并且随着样本数量的组合增长,这种交叉验证方法的成本可能非常高。对于大型数据集,应该倾向于 KFold , StratifiedKFoldShuffleSplit .

阅读更多的 User Guide .

参数:
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()[源代码]#

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

请检查 User Guide 关于路由机制如何工作。

返回:
routingMetadataRequest

A MetadataRequest 封装路由信息。

get_n_splits(X, y=None, groups=None)[源代码]#

返回交叉验证器中分裂迭代的数量。

参数:
X形状类似阵列(n_samples,n_features)

训练数据,在哪里 n_samples 是样本数量和 n_features 是功能的数量。

y对象

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

groups对象

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

split(X, y=None, groups=None)[源代码]#

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

参数:
X形状类似阵列(n_samples,n_features)

训练数据,在哪里 n_samples 是样本数量和 n_features 是功能的数量。

y形状类似阵列(n_samples,)

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

groups对象

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

收益率:
trainndarray

训练为该分裂设置了指数。

testndarray

测试为该分裂设置了指数。