sklearn.model_selection.RepeatedKFold?
class sklearn.model_selection.RepeatedKFold(*, n_splits=5, n_repeats=10, random_state=None)
[源碼]
重復的K折交叉驗證器。
重復K折n次,每次重復具有不同的隨機性。
在用戶指南中閱讀更多內容。
參數 | 說明 |
---|---|
n_splits | int, default=5 折數。必須至少為2。 |
n_repeats | int, default=10 交叉驗證器需要重復的次數。 |
random_state | int or RandomState instance, default=None 控制每次重復的交叉驗證實例的隨機性。為多個函數調用傳遞可重復輸出的int值。請參閱詞匯表。 |
另見:
RepeatedStratifiedKFold
重復分層K折n次。
注
隨機CV切分器可能會為每個分割調用返回不同的結果。您可以通過設置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)
>>> for train_index, test_index in rkf.split(X):
... print("TRAIN:", train_index, "TEST:", test_index)
... X_train, X_test = X[train_index], X[test_index]
... y_train, y_test = y[train_index], y[test_index]
...
TRAIN: [0 1] TEST: [2 3]
TRAIN: [2 3] TEST: [0 1]
TRAIN: [1 2] TEST: [0 3]
TRAIN: [0 3] TEST: [1 2]
方法
方法 | 說明 |
---|---|
get_n_splits (self[, X, y, groups]) |
返回交叉驗證器中的拆分迭代次數 |
split (self, X[, y, groups]) |
生成索引以將數據分為訓練集和測試集。 |
__init__(self,*,n_splits = 5,n_repeats = 10,random_state = None )
[源碼]
初始化self。詳情可參閱 type(self)的幫助。
get_n_splits(self,X = None,y = None,groups = None )
[源碼]
返回交叉驗證器中的切分迭代次數。
參數 | 說明 |
---|---|
X | object 始終被忽略,為了兼容性而存在。 np.zeros(n_samples) 可用作占位符。 |
y | object 始終被忽略,為了兼容性而存在。 np.zeros(n_samples) 可用作占位符。 |
groups | array-like of shape (n_samples,), default=None 將數據集切分為訓練集或測試集時使用的樣本的分組標簽。 |
返回值 | 說明 |
---|---|
n_splits | int 返回交叉驗證器中拆分迭代的次數。 |
split(self,X,y = None,groups = None )
[源碼]
生成索引以將數據分為訓練集和測試集。
參數 | 說明 |
---|---|
X | array-like, shape (n_samples, n_features) 用于訓練的數據,其中n_samples是樣本數量,n_features是特征數量。 |
y | array-like of length n_samples 監督學習問題的目標變量。 |
groups | array-like of shape (n_samples,), default=None 將數據集拆分為訓練集或測試集時使用的樣本的分組標簽。 |
輸出 | 說明 |
---|---|
train | ndarray 切拆分的訓練集索引。 |
test | ndarray 切分的測試集索引。 |