留 P 交叉驗證#
- class sklearn.model_selection.LeavePOut(p)[原始碼]#
留 P 交叉驗證器。
提供訓練/測試索引,以將資料分割成訓練/測試集。這會導致測試大小為 p 的所有不同樣本,而剩餘的 n - p 個樣本在每次迭代中形成訓練集。
注意:
LeavePOut(p)
不等同於KFold(n_splits=n_samples // p)
,後者會建立不重疊的測試集。由於迭代次數隨樣本數量呈組合式增長,因此這種交叉驗證方法可能非常昂貴。對於大型資料集,應優先選擇
KFold
、StratifiedKFold
或ShuffleSplit
。請在 使用者指南中閱讀更多資訊。
- 參數:
- 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
,封裝了路由資訊。