sklearn.model_selection.TimeSeriesSplit?
class sklearn.model_selection.TimeSeriesSplit(n_splits=5, *, max_train_size=None)
[源碼]
時間序列交叉驗證器
版本0.18中的新功能。
提供訓練集或測試集索引,以切分在訓練集或測試集中以固定時間間隔觀察到的時間序列數據樣本。在每個分組中,測試集索引必須比以前更高,因此在交叉驗證器中打亂是不合適的。
此交叉驗證對象是KFold
的變體。在第k個切分中,它返回前k個折疊作為訓練集,第(k + 1)個折疊作為測試集。
請注意,與標準的交叉驗證方法不同,連續的訓練集是它們之前的訓練集的超集。
在用戶指南中閱讀更多內容。
參數 | 說明 |
---|---|
n_splits | int, default=5 分割數。必須至少為2。 0.22版中: n_splits 默認值從3更改為5。 |
max_train_size | int, default=None 單個訓練集的最大大小。 |
注
訓練集的大小為i``th split, with a test set of size ``n_samples//(n_splits + 1)
,在i * n_samples // (n_splits + 1) + n_samples % (n_splits + 1)
中,n_samples
為樣本數量。
示例
>>> import numpy as np
>>> from sklearn.model_selection import TimeSeriesSplit
>>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4], [1, 2], [3, 4]])
>>> y = np.array([1, 2, 3, 4, 5, 6])
>>> tscv = TimeSeriesSplit()
>>> print(tscv)
TimeSeriesSplit(max_train_size=None, n_splits=5)
>>> for train_index, test_index in tscv.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] TEST: [1]
TRAIN: [0 1] TEST: [2]
TRAIN: [0 1 2] TEST: [3]
TRAIN: [0 1 2 3] TEST: [4]
TRAIN: [0 1 2 3 4] TEST: [5]
方法
方法 | 說明 |
---|---|
get_n_splits (self[, X, y, groups]) |
返回交叉驗證器中的切分迭代次數 |
split (self, X[, y, groups]) |
生成索引以將數據分為訓練集和測試集。 |
__init__(self, n_splits=5, *, max_train_size=None)
[源碼]
初始化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,y = None,groups = None )
[源碼]
生成索引以將數據分為訓練集和測試集。
參數 | 說明 |
---|---|
X | array-like of shape (n_samples, n_features) 用于訓練的數據,其中n_samples是樣本數量,n_features是特征數量。 |
y | array-like of shape (n_samples,) 始終被忽略,為了兼容性而存在。 |
groups | array-like of shape (n_samples,) 始終被忽略,為了兼容性而存在。 |
輸出 | 說明 |
---|---|
train | ndarray 切分的訓練集索引。 |
test | ndarray 切分的測試集索引。 |