重複 K 折交叉驗證 (RepeatedKFold)#
- class sklearn.model_selection.RepeatedKFold(*, n_splits=5, n_repeats=10, random_state=None)[來源]#
重複 K 折交叉驗證器。
在每次重複中,以不同的隨機化重複 K 折 n 次。
請在使用者指南中閱讀更多內容。
- 參數:
- n_splitsint,預設值=5
折疊數。必須至少為 2。
- n_repeatsint,預設值=10
交叉驗證器需要重複的次數。
- random_stateint、RandomState 實例或 None,預設值=None
控制每次重複交叉驗證實例的隨機性。傳遞一個 int 以便在多個函數呼叫中產生可重現的輸出。請參閱詞彙表。
另請參閱
RepeatedStratifiedKFold
重複分層 K 折 n 次。
注意事項
隨機化的交叉驗證分割器可能會為每次分割呼叫傳回不同的結果。您可以透過將
random_state
設定為整數來使結果相同。範例
>>> import numpy as np >>> from sklearn.model_selection import RepeatedKFold >>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]]) >>> y = np.array([0, 0, 1, 1]) >>> rkf = RepeatedKFold(n_splits=2, n_repeats=2, random_state=2652124) >>> rkf.get_n_splits(X, y) 4 >>> print(rkf) RepeatedKFold(n_repeats=2, n_splits=2, random_state=2652124) >>> for i, (train_index, test_index) in enumerate(rkf.split(X)): ... print(f"Fold {i}:") ... print(f" Train: index={train_index}") ... print(f" Test: index={test_index}") ... Fold 0: Train: index=[0 1] Test: index=[2 3] Fold 1: Train: index=[2 3] Test: index=[0 1] Fold 2: Train: index=[1 2] Test: index=[0 3] Fold 3: Train: index=[0 3] Test: index=[1 2]
- get_metadata_routing()[來源]#
取得此物件的中繼資料路由。
請查看使用者指南以了解路由機制的運作方式。
- 傳回值:
- routingMetadataRequest
一個封裝路由資訊的
MetadataRequest
。