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

如何查询sktime的TimeSeriesForestClassifier计算特征对应的输入区间

问题解答

特征重要性长度与输入不一致的原因

TimeSeriesForestClassifier不会直接对原始时间点计算重要性,它的核心逻辑是从输入序列中随机采样多个时间区间,对每个区间提取均值、标准差、斜率三个统计特征,再用这些统计特征训练随机森林。feature_importances_返回的就是这些统计特征的重要性,而非原始时间点的重要性,所以长度和原始输入序列长度不相等。

你代码中使用了ColumnConcatenator,会先将所有维度的多元时间序列按时间维度拼接为单变量长序列,再送入分类器,所以采样的区间是基于拼接后的序列生成的。

区间信息获取方法

训练完成的TimeSeriesForestClassifier的每个基决策树都保存了采样区间的元数据,可通过estimators_属性访问,具体使用方式如下:

import pandas as pd

# 取出Pipeline中的TimeSeriesForestClassifier实例
tsf = clf.steps[1][1]
# 原始数据单个维度的时间步长度,basic_motions数据集默认是100
n_timepoints = X_train.iloc[0, 0].shape[0]
# 统计量类型映射
stat_map = {0: "均值", 1: "标准差", 2: "斜率"}

feature_mapping = []
for tree_idx, tree in enumerate(tsf.estimators_):
    # tree.intervals_的每一行对应该树一个统计特征的元信息:[拼接后维度索引, 区间起始, 区间结束, 统计量索引]
    for feat_idx, interval in enumerate(tree.intervals_):
        _, concat_start, concat_end, stat_idx = interval
        # 换算回原始多元时间序列的对应维度
        orig_dim = concat_start // n_timepoints
        # 计算区间在原始维度内的位置
        dim_start = concat_start % n_timepoints
        dim_end = concat_end % n_timepoints
        feature_mapping.append({
            "所属基树索引": tree_idx,
            "特征索引": feat_idx,
            "原始维度索引": orig_dim,
            "原始维度内区间起始": dim_start,
            "原始维度内区间结束": dim_end,
            "统计量类型": stat_map[stat_idx],
            "该特征在当前树的重要性": tree.feature_importances_[feat_idx]
        })

# 全局feature_importances_是所有基树的特征重要性平均后归一化的结果,可根据上述映射表自行聚合对应
global_importance = tsf.feature_importances_

如果不需要对每个维度单独处理,也可以去掉ColumnConcatenator改用ColumnEnsembleClassifier对每个维度单独训练分类器,此时每个维度分类器的区间信息不需要额外换算,直接对应所属维度的时间区间。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 12:06:05