Package: timebasedcv
Time based cross validationtimebasedcvis a Python codebase that provides a cross validation strategy based on time.Documentation:https://fbruzzesi.github.io/timebasedcvSource Code:https://github.com/fbruzzesi/timebasedcvAlpha NoticeThis codebase is experimental and is working for my use cases. It is very probable that there are cases not covered and for which it breaks (badly). If you find them, please feel free to open an issue in theissue pageof the repo.DescriptionThe current implementation ofscikit-learn TimeSeriesSplitlacks the flexibility of having multiple samples within the same time period/unit.This codebase addresses such problem by providing a cross validation strategy based on a time period rather than the number of samples. This is useful when the data is time dependent, and the model should be trained on past data and tested on future data, independently from the number of observations present within a given time period.We introduce two main classes:TimeBasedSplit: a class that allows to define a time based split with a given frequency, train size, test size, gap, stride and window type. It's core methodsplitrequires to pass a time series as input to create the boolean masks for train and test from the instance information defined above. Therefore it is not compatible withscikit-learn CV Splitters.TimeBasedCVSplitter: a class that conforms with scikit-learn CV Splitters but requires to pass the time series as input to the instance. That is because a CV Splitter needs to know a priori the number of splits and thesplitmethod shouldn't take any extra arguments as input other than the arrays to split.Installationtimebasedcvis a published Python package onpypi, therefore it can be installed directly via pip, as well as from source using pip and git, or with a local clone:pip:python -m pip install timebasedcv(suggested)pip + source/git:python -m pip install git+https://github.com/FBruzzesi/timebasedcv.gitlocal clone:gitclonehttps://github.com/FBruzzesi/timebasedcv.gitcdtimebasedcv
python-mpipinstall.QuickstartAs aquickstart, you can use the following code snippet to get started.
Consider checkout out theGetting Startedsection of for a detailed guide on how to use the library.First let's generate some data with different number of points per day:importpandasaspdimportnumpyasnpnp.random.seed(42)dates=pd.Series(pd.date_range("2023-01-01","2023-01-31",freq="D"))size=len(dates)df=pd.concat([pd.DataFrame({"time":pd.date_range(start,end,periods=_size,inclusive="left"),"value":np.random.randn(_size-1)/25,})forstart,end,_sizeinzip(dates[:size],dates[1:],np.random.randint(2,24,size-1))]).reset_index(drop=True)time_series,X=df["time"],df["value"]df.set_index("time").resample("D").count().head(5)# time	        value# 2023-01-01	14# 2023-01-02	2# 2023-01-03	22# 2023-01-04	11# 2023-01-05	1Now let's run the split with a given frequency, train size, test size, gap, stride and window type:fromtimebasedcvimportTimeBasedSplitconfigs=[{"frequency":"days","train_size":14,"forecast_horizon":7,"gap":2,"stride":5,"window":"expanding"},...]tbs=TimeBasedSplit(**config,)fmt="%Y-%m-%d"fortrain_set,forecast_setintbs.split(X,time_series=time_series):# Do some magic hereLet's see howtrain_setandforecasting_setsplits would look likes for different split strategies (or configurations).The green dots represent the train points, while the red dots represent the forecastng points.ContributingPlease read theContributing guidelinesin the documentation site.LicenseThe project has aMIT Licence