时间序列数据的交叉验证:将用户定义的元组列表与内部列表列表转换为元组列表以应用于 GridSearchCV

Cross-validation for time-series data: Convert user-defined list of tuples with inner lists of lists to list of tuples for applying in GridSearchCV

我有时间序列数据,想在 Python 中对我的 ML 模型进行前向交叉验证。为了创建拆分,我做了以下操作:

cv_split = [(list_of_lists[:i], list_of_lists[i:i+1]) for i in range(1, len(list_of_lists))] 

(其中 list_of_lists 例如:[[0,1,2],[3,4],[5,6,7,8,], ...]
其中每个列表代表特定年份的观察结果。

cv_split 的结果是带有内部列表列表的元组列表,每个元组是:([[0,1,2],[3,4]], [[5,6,7,8]]),
这就是问题所在,因为 GridSearchCV 不接受这个。

我知道我的 cv_split 可以使用以下表格:
([0,1,2,3,4], [5,6,7,8]) (list of tuples of lists).
好吧,我挣扎着如何从 ([[0,1,2],[3,4]], [[5,6,7,8]]) 变成 ([0,1,2,3,4], [5,6,7,8])?

这里比较全面:

现在我有:

[([[0,1,2],[3,4]], [[5,6,7,8]]) 

([[0,1,2],[3,4],[5,6,7,8]],[[9,10]])

([[0,1,2],[3,4],[5,6,7,8],[9,10]],[[11,12,13]]) 

([[0,1,2],[3,4],[5,6,7,8],[9,10],[11,12,13]],[[14,15,16]])] 

我需要以下表格:

[([0,1,2,3,4], [5,6,7,8]) 

([0,1,2,3,4,5,6,7,8],[9,10]) 

([0,1,2,3,4,5,6,7,8,9,10],[11,12,13]) 

([0,1,2,3,4,5,6,7,8,9,10,11,12,13],[14,15,16])]

我是 Python 的新手,如果能提供一些解释,我会很高兴。

以下是使用嵌套列表理解的方法:

lst = ([[0,1,2],[3,4]], [[5,6,7,8]])

t = tuple([[a for b in l for a in b] for l in lst])

print(t)

输出:

([0, 1, 2, 3, 4], [5, 6, 7, 8])

更新:

lst = [([[0,1,2],[3,4]], [[5,6,7,8]]),
       ([[0,1,2],[3,4],   [5,6,7,8]],[[9,10]]),
       ([[0,1,2],[3,4],   [5,6,7,8],  [9,10]],[[11,12,13]]),
       ([[0,1,2],[3,4],   [5,6,7,8],  [9,10],  [11,12,13]],[[14,15,16]])]

ls = [tuple([[a for b in l for a in b] for l in tt]) for tt in lst]