Sklearn : Get last split from timeSeriesSplit
Sklearn : Get last split from timeSeriesSplit
So I am using the timeSeriesSplit from sklearn to split my data like this,
tscv = TimeSeriesSplit(n_splits=3)
Now I know in order to get the split indices we have to iterate over tscv.split(X). My question here is, is it possible to get directly to the last split, without iterating over the splits. The object returned by the function is not exactly a list, so I am not sure how to do this ? I need the last split only, since my data is large and no. of splits is also large.
tscv.split(X)
Thanks in advance
2 Answers
2
You can try this
def get_last_cv(splits):
splits_deque = deque(splits, maxlen=1)
last_element = splits_deque.pop()
train,test = last_element
return train,test
and then get the indices like this
train_index,test_index = get_last_cv(tscv.split(X))
where X is your data
X
Split method in TimeSeriesSplit generates split of indices. In order to get to specific split you need to iterate to it. It is made to iterate over all possible splits for time-series cross validation.
TimeSeriesSplit
If size of the test data in the cv split equals s. Then, no matter how many splits you make, last split sets train_data = all data except last s data point and test_data as last s data points. So, if you want the last split directly: Slice your data. For eg. if you data is a numpy array X:
s
train_data
all data except last s data point
test_data as last s data points
X
import numpy as np
from sklearn.model_selection import TimeSeriesSplit
X = np.array([[1, 2], [0, 4], [1, 2], [2, 4] ,[1, 2], [7, 4], [8, 2], [5, 4]])
n_splits = 2 # select no of splits required
tscv = TimeSeriesSplit(n_splits = n_splits)
n_samples = X.shape[0] # this is how test_size (s)
s = n_samples//(n_splits + 1) # is evaluated internally
X_train_last, X_test_last = X[ :-s], X[-s: ] # s=2 for this split
X_train_last
# array([[1, 2],
# [0, 4],
# [1, 2],
# [2, 4],
# [1, 2],
# [7, 4]])
X_test_last
# array([[8, 2],
# [5, 4]])
Also, if you have set the "max_train_size", while splitting. Then you need to take care of that too while slicing. Refer TimeSeriesSplit documentation here for details.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.