You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

sktime分类训练报错:样本数显示匹配却提示不匹配

sktime分类任务样本数量不匹配报错问题解析

问题场景

使用sktime执行分类任务时,数据集拆分后X_train与y_train样本数一致,但训练阶段出现样本数量不匹配的报错。

数据拆分代码及输出

from sklearn.model_selection import train_test_split
X = AUDCHF_h1_model[['Open','High','Low','Close','Volume','VWMA',
                                   'Minute','Hour','Day','Week','Month','Year']].values
y = AUDCHF_h1_model[['is_beg_leg']].values

X_train,X_test,y_train,y_test = train_test_split(
    X, y, test_size=0.2)

print(X_train.shape, y_train.shape, X_test.shape, y_test.shape)

执行输出:

(53250, 12) (53250, 1) (13313, 12) (13313, 1)

训练代码及报错

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline

from sktime.classification.compose import ColumnEnsembleClassifier
from sktime.classification.dictionary_based import BOSSEnsemble
from sktime.classification.interval_based import TimeSeriesForestClassifier
#from sktime.classification.shapelet_based import MrSEQLClassifier
from sktime.datasets import load_basic_motions
from sktime.transformations.panel.compose import ColumnConcatenator

steps = [
    ("concatenate", ColumnConcatenator()),
    ("classify", TimeSeriesForestClassifier(n_estimators=100)),
]
clf = Pipeline(steps)
clf.fit(X_train, y_train)
clf.score(X_test, y_test)

报错信息:

ValueError: Mismatch in number of cases. Number in X = 639000 nos in y = 53250

手动确认数据集形状:

X_train.shape  # 输出(53250, 12)
y_train.shape  # 输出(53250, 1)

问题原因

sktime的时间序列分类器(如TimeSeriesForestClassifier)和ColumnConcatenator要求输入的X为三维面板数据格式:(n_samples, n_features, n_timepoints)。而当前的X_train是二维数组(53250, 12),sktime会错误地将其解析为「12个样本,每个样本包含53250个时间点」,导致计算出的总样本数为53250*12=639000,与y的53250个样本数不匹配。

本质是sktime面板分类器对二维数组的解析逻辑,和传统机器学习的「样本-特征」二维结构完全相反。

解决方案

将二维特征矩阵转换为sktime要求的三维面板格式,有两种可行方法:

方法1:直接调整数组维度

把(n_samples, n_features)的二维数组转换为(n_samples, n_features, 1)的三维数组(因为每个特征对应单个时间步):

# 转换训练集和测试集维度
X_train_sktime = X_train.reshape((X_train.shape[0], X_train.shape[1], 1))
X_test_sktime = X_test.reshape((X_test.shape[0], X_test.shape[1], 1))

# 使用转换后的数组训练模型
clf.fit(X_train_sktime, y_train)
clf.score(X_test_sktime, y_test)

方法2:使用sktime转换工具

通过TabularToPanelTransformer将表格数据自动转换为面板格式:

from sktime.transformations.panel.tabular import TabularToPanelTransformer

# 修改Pipeline,增加格式转换步骤
steps = [
    ("tabular_to_panel", TabularToPanelTransformer()),
    ("concatenate", ColumnConcatenator()),
    ("classify", TimeSeriesForestClassifier(n_estimators=100)),
]
clf = Pipeline(steps)
clf.fit(X_train, y_train)
clf.score(X_test, y_test)

额外提示

如果你的数据是传统表格型数据(非时间序列面板),更适合直接使用scikit-learn的分类器。sktime的分类器核心是为「单变量/多变量时间序列样本」设计的,每个样本对应一段连续的时间序列,而非单个时间点的多特征数据。

内容的提问来源于stack exchange,提问作者jack

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 19:54:26