sklearn.model_selection.LeavePOut?
class sklearn.model_selection.LeavePOut(p)
[源碼]
留P法交叉驗證器。
提供訓練集或測試集的索引以將數據切分為訓練集或測試集。這導致對大小為p的所有不同樣本進行測試,而其余n-p個樣本在每次迭代中形成訓練集。
注意:LeavePOut(p)
不等同于創建不重疊的測試集的KFold(n_splits=n_samples // p)
。
由于大量的迭代隨樣本數量一起增長,因此這種交叉驗證方法可能會非常耗時。對于大型數據集應該偏向于KFold
,StratifiedKFold
或ShuffleSplit
。
在用戶指南中閱讀更多內容。
參數 | 說明 |
---|---|
p | int 測試集的大小。必須嚴格少于樣本數量。 |
示例
>>> 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 train_index, test_index in lpo.split(X):
... print("TRAIN:", train_index, "TEST:", test_index)
... X_train, X_test = X[train_index], X[test_index]
... y_train, y_test = y[train_index], y[test_index]
TRAIN: [2 3] TEST: [0 1]
TRAIN: [1 3] TEST: [0 2]
TRAIN: [1 2] TEST: [0 3]
TRAIN: [0 3] TEST: [1 2]
TRAIN: [0 2] TEST: [1 3]
TRAIN: [0 1] TEST: [2 3]
方法
方法 | 說明 |
---|---|
get_n_splits (self, X[, y, groups]) |
返回交叉驗證器中的拆分迭代次數 |
split (self, X[, y, groups]) |
生成索引以將數據分為訓練集和測試集。 |
__init__(self,p )
[源碼]
初始化self。詳情可參閱 type(self)的幫助。
get_n_splits(self,X,y = None,groups = None )
[源碼]
返回交叉驗證器中的切分迭代次數。
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_features) 用于訓練的數據,其中n_samples是樣本數量,n_features是特征數量。 |
y | object 始終被忽略,為了兼容性而存在。 |
groups | object 始終被忽略,為了兼容性而存在。 |
split(self,X,y = None,groups = None )
[源碼]
生成索引以將數據分為訓練集和測試集。
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_features) 用于訓練的數據,其中n_samples是樣本數量,n_features是特征數量。 |
y | array-like of shape (n_samples,) 監督學習問題的目標變量。 |
groups | array-like of shape (n_samples,), default=None 將數據集拆分為訓練集或測試集時使用的樣本的分組標簽。 |
輸出 | 說明 |
---|---|
train | ndarray 切分的訓練集索引。 |
test | ndarray 切分的測試集索引。 |