scikit-learn cross validation custom splits for time series data

后端 未结 3 527
不知归路
不知归路 2021-01-31 12:12

I\'d like to use scikit-learn\'s GridSearchCV to determine some hyper parameters for a random forest model. My data is time dependent and looks something like

i         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-31 12:34

    There's standard sklearn approach to that, using GroupShuffleSplit. From the docs:

    Provides randomized train/test indices to split data according to a third-party provided group. This group information can be used to encode arbitrary domain specific stratifications of the samples as integers.

    For instance the groups could be the year of collection of the samples and thus allow for cross-validation against time-based splits.

    Very much convenient for your use case. Here how it looks like:

    cv = GroupShuffleSplit().split(X, y, groups)
    

    And passing that to GridSearchCV like before:

    GridSearchCV(estimator, param_grid, cv=cv, ...)
    

提交回复
热议问题