留 P 交叉驗證#

class sklearn.model_selection.LeavePOut(p)[原始碼]#

留 P 交叉驗證器。

提供訓練/測試索引,以將資料分割成訓練/測試集。這會導致測試大小為 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()[原始碼]#

取得此物件的中繼資料路由。

請參考 使用者指南 以了解路由機制如何運作。

回傳值:
routingMetadataRequest

一個 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

該分割的測試集索引。