LeaveOneOut#

class sklearn.model_selection.LeaveOneOut[source]#

Leave-One-Out cross-validator. (留一交叉驗證器。)

提供訓練/測試索引,以將資料分割為訓練集和測試集。每個樣本都會被用作一次測試集(單例),而剩餘的樣本則構成訓練集。

注意:LeaveOneOut() 等同於 KFold(n_splits=n)LeavePOut(p=1),其中 n 是樣本數。

由於測試集的數量很多(與樣本數相同),這種交叉驗證方法可能會非常耗費資源。對於大型資料集,應優先選擇 KFoldShuffleSplitStratifiedKFold

請在使用者指南中閱讀更多內容。

另請參閱

LeaveOneGroupOut (留一組交叉驗證)

用於根據資料集的明確、特定領域的分層來分割資料。

GroupKFold (分組 K 折交叉驗證)

具有非重疊組的 K 折疊迭代器變體。

範例

>>> import numpy as np
>>> from sklearn.model_selection import LeaveOneOut
>>> X = np.array([[1, 2], [3, 4]])
>>> y = np.array([1, 2])
>>> loo = LeaveOneOut()
>>> loo.get_n_splits(X)
2
>>> print(loo)
LeaveOneOut()
>>> for i, (train_index, test_index) in enumerate(loo.split(X)):
...     print(f"Fold {i}:")
...     print(f"  Train: index={train_index}")
...     print(f"  Test:  index={test_index}")
Fold 0:
  Train: index=[1]
  Test:  index=[0]
Fold 1:
  Train: index=[0]
  Test:  index=[1]
get_metadata_routing()[原始碼]#

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

請查看使用者指南,了解路由機制如何運作。

回傳值:
routingMetadataRequest

一個封裝路由資訊的 MetadataRequest

get_n_splits(X, y=None, groups=None)[原始碼]#

回傳交叉驗證器中的分割迭代次數。

參數:
X類陣列,形狀為 (n_samples, n_features)

訓練資料,其中 n_samples 是樣本數,n_features 是特徵數。

y物件

始終被忽略,存在是為了相容性。

groups物件

始終被忽略,存在是為了相容性。

回傳值:
n_splits整數

回傳交叉驗證器中的分割迭代次數。

split(X, y=None, groups=None)[原始碼]#

產生將資料分割為訓練集和測試集的索引。

參數:
X類陣列,形狀為 (n_samples, n_features)

訓練資料,其中 n_samples 是樣本數,n_features 是特徵數。

y類陣列,形狀為 (n_samples,)

用於監督式學習問題的目標變數。

groups物件

始終被忽略,存在是為了相容性。

產生值:
trainndarray

該分割的訓練集索引。

testndarray

該分割的測試集索引。