如何将RandomSearch与按时间段时间序列分组的交叉验证一起使用

问题描述

我想知道是否可以用cv执行sklearn网格搜索

示例代码

gs = RandomizedSearchCV(
    estimator=clf,param_distributions=param_test,n_iter=10,cv=4,refit=True,random_state=123,verbose=True)

其中CV =时间序列分割

class timeseriessplit(_BaseKFold):
    def __init__(self,n_splits=5):
        super().__init__(n_splits,shuffle=False,random_state=None)

    def split(self,X,y=None,groups=None):
        X,y,groups = indexable(X,groups)
        n_samples = _num_samples(X)
        n_splits = self.n_splits
        n_folds = n_splits + 1
        group_list = np.unique(groups)
        n_groups = len(group_list)
        if n_folds > n_groups:
            raise ValueError(
                ("Cannot have number of folds ={0} greater"
                 " than the number of samples: {1}.").format(n_folds,n_groups))
        indices = np.arange(n_samples)
        test_size = (n_groups // n_folds)
        test_starts = range(test_size + n_groups % n_folds,n_groups,test_size)
        test_starts = list(test_starts)[::-1]
        for test_start in test_starts:
            
            yield (indices[groups.isin(group_list[:test_start])],indices[groups.isin(group_list[test_start:test_start + test_size])])

但是,它显示一个错误

折叠倍数= 6不能大于样本数:1

有什么建议吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)