LeaveOneOut#
- class sklearn.model_selection.LeaveOneOut[source]#
Leave-One-Out cross-validator. (留一交叉驗證器。)
提供訓練/測試索引,以將資料分割為訓練集和測試集。每個樣本都會被用作一次測試集(單例),而剩餘的樣本則構成訓練集。
注意:
LeaveOneOut()
等同於KFold(n_splits=n)
和LeavePOut(p=1)
,其中n
是樣本數。由於測試集的數量很多(與樣本數相同),這種交叉驗證方法可能會非常耗費資源。對於大型資料集,應優先選擇
KFold
、ShuffleSplit
或StratifiedKFold
。請在使用者指南中閱讀更多內容。
另請參閱
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
。