sklearn.model_selection.PredefinedSplit?
class sklearn.model_selection.PredefinedSplit(test_fold)
[源碼]
預定義的切分交叉驗證器。
提供訓練集或測試集的索引,使用用戶通過test_fold
參數指定的預定義方案將數據分為訓練集或測試集。
在用戶指南中閱讀更多內容。
版本0.16中的新功能。
參數 | 說明 |
---|---|
test_fold | array-like of shape (n_samples,) 輸入 test_fold[i] 表示樣本i 所屬的測試集的索引。通過將test_fold[i] 值設置為-1,可以從任何測試集中排除樣本i (即在每個訓練集中包括樣本i )。 |
示例
>>> import numpy as np
>>> from sklearn.model_selection import PredefinedSplit
>>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])
>>> y = np.array([0, 0, 1, 1])
>>> test_fold = [0, 1, -1, 1]
>>> ps = PredefinedSplit(test_fold)
>>> ps.get_n_splits()
2
>>> print(ps)
PredefinedSplit(test_fold=array([ 0, 1, -1, 1]))
>>> for train_index, test_index in ps.split():
... 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: [1 2 3] TEST: [0]
TRAIN: [0 2] TEST: [1 3]
方法
方法 | 說明 |
---|---|
get_n_splits (self[, X, y, groups]) |
返回交叉驗證器中的拆分迭代次數。 |
split (self[, X, y, groups]) |
生成索引以將數據分為訓練集和測試集。 |
__init__(self, test_fold)
[源碼]
初始化self。詳情可參閱 type(self)的幫助。
get_n_splits(self,X = None,y = None,groups = None )
[源碼]
返回交叉驗證器中的切分迭代次數。
參數 | 說明 |
---|---|
X | object 始終被忽略,為了兼容性而存在。 |
y | object 始終被忽略,為了兼容性而存在。 |
groups | object 始終被忽略,為了兼容性而存在。 |
返回值 | 說明 |
---|---|
n_splits | int 返回交叉驗證器中拆分迭代的次數。 |
split(self,X = None,y = None,groups = None )
[源碼]
生成索引以將數據分為訓練和測試集。
參數 | 說明 |
---|---|
X | object 始終被忽略,為了兼容性而存在。 |
y | object 始終被忽略,為了兼容性而存在。 |
groups | object 始終被忽略,為了兼容性而存在。 |
輸出 | 說明 |
---|---|
train | ndarray 切分的訓練集索引。 |
test | ndarray 切分的測試集索引。 |