重複 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

get_n_splits(X=None, y=None, groups=None)[來源]#

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

參數:
X物件

始終忽略,為相容性而存在。np.zeros(n_samples) 可用作佔位符。

y物件

始終忽略,為相容性而存在。np.zeros(n_samples) 可用作佔位符。

groups形狀為 (n_samples,) 的類陣列,預設值=None

在將資料集分割成訓練/測試集時,用於樣本的群組標籤。

傳回值:
n_splitsint

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

split(X, y=None, groups=None)[來源]#

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

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

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

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

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

groups物件

始終忽略,為相容性而存在。

產生:
trainndarray

該分割的訓練集索引。

testndarray

該分割的測試集索引。