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

如何将自定义特征计算器添加至tsfresh.feature_extraction.feature_calculators子模块?

解决tsfresh自定义特征计算器未被识别的问题

要让tsfresh识别你的自定义特征函数zero_value_percentage,必须将其显式添加到tsfresh.feature_extraction.feature_calculators模块中,具体操作步骤如下:

步骤1:完整导入feature_calculators模块

不要仅导入set_property,需同时导入整个特征计算器模块:

from tsfresh.feature_extraction.feature_calculators import set_property
import tsfresh.feature_extraction.feature_calculators as fc

步骤2:将自定义函数挂载到模块

在定义完zero_value_percentage函数后,把它赋值为模块的属性,完成注册:

@set_property("fctype", "simple")
def zero_value_percentage(x):
    ratio = (x==0).sum()/x.shape[0]
    return ratio

# 关键操作:将自定义函数注册到feature_calculators模块
fc.zero_value_percentage = zero_value_percentage

步骤3:正常调用特征提取

之后你的fc_parameters配置和extract_features调用即可正常执行。

修改后的完整代码示例

import pandas as pd,numpy as np
import tsfresh
from tsfresh.feature_extraction.feature_calculators import set_property
import tsfresh.feature_extraction.feature_calculators as fc

@set_property("fctype", "simple")
def zero_value_percentage(x):
    ratio = (x==0).sum()/x.shape[0]
    return ratio

# 注册自定义特征计算器
fc.zero_value_percentage = zero_value_percentage

fc_parameters = {'mean':None, 'standard_deviation':None,  'zero_value_percentage':None}

df_features = tsfresh.extract_features(
    data[['id','year_month','order_qty']], 
    column_id='id', 
    column_sort='year_month', 
    default_fc_parameters=fc_parameters
)    

原理说明

tsfresh执行特征提取时,会根据你传入的特征名称,在feature_calculators模块内查找对应的函数。因此必须将自定义函数挂载到该模块,才能让系统识别并调用它。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 10:05:35