如何添加自定义功能
如果你想从你的时间序列中提取自定义特征,tsfresh 允许你通过几个简单的步骤来实现:
步骤 1. 决定你要实现的功能类型
tsfresh 支持两种类型的特征计算方法:
1. 简单
2. 组合器
区别在于为单个时间序列计算的特征数量。如果 feature_calculator 返回一个 (1.) 特征,那么它是简单的;如果它返回多个特征 (2.),那么它是一个组合器。因此,如果你想添加一个单一特征,你应该选择 1.,即简单的特征计算器类。然而,如果同时计算多个特征更好(例如,只为所有特征执行一次辅助计算),那么你应该选择类型 2.。
步骤2. 编写特征计算器
根据您正在实现的特征计算器类型,您可以使用以下特征计算器框架:
1. 简单功能
你可以编写一个简单的特征计算器,返回一个特征,不带参数,如下所示:
from tsfresh.feature_extraction.feature_calculators import set_property
@set_property("fctype", "simple")
def your_feature_calculator(x):
"""
The description of your feature
:param x: the time series to calculate the feature of
:type x: pandas.Series
:return: the value of this feature
:return type: bool, int or float
"""
# Calculation of feature as float, int or bool
result = f(x)
return result
或带有参数:
@set_property("fctype", "simple"")
def your_feature_calculator(x, p1, p2, ...):
"""
Description of your feature
:param x: the time series to calculate the feature of
:type x: pandas.Series
:param p1: description of your parameter p1
:type p1: type of your parameter p1
:param p2: description of your parameter p2
:type p2: type of your parameter p2
...
:return: the value of this feature
:return type: bool, int or float
"""
# Calculation of feature as float, int or bool
f = f(x)
return f
2. 组合器特性
或者,你可以编写一个组合特征计算器,如下所示返回多个特征:
from tsfresh.utilities.string_manipulation import convert_to_output_format
@set_property("fctype", "combiner")
def your_feature_calculator(x, param):
"""
Short description of your feature (should be a one liner as we parse the first line of the description)
Long detailed description, add somme equations, add some references, what kind of statistics is the feature
capturing? When should you use it? When not?
:param x: the time series to calculate the feature of
:type x: pandas.Series
:param c: the time series name
:type c: str
:param param: contains dictionaries {"p1": x, "p2": y, ...} with p1 float, p2 int ...
:type param: list
:return: list of tuples (s, f) where s are the parameters, serialized as a string,
and f the respective feature value as bool, int or float
:return type: pandas.Series
"""
# Do some pre-processing if needed for all parameters
# f is a function that calculates the feature value for each single parameter combination
return [(convert_to_output_format(config), f(x, config)) for config in param]
编写自己的基于时间的特征计算器
编写自己的基于时间的特征计算器与平时没有什么不同。只需使用 @set_property 装饰器设置两个新属性:
添加
@set_property("input", "pd.Series")告诉函数输入是pd.Series而不是numpy数组。这允许自动使用索引。添加
@set_property("index_type", pd.DatetimeIndex)告诉函数输入是一个 DatetimeIndex,使其能够基于时间数据类型执行计算。
例如,如果我们想编写一个计算第一次和最后一次测量之间时间的函数,它可能看起来像这样:
@set_property("input", "pd.Series")
@set_property("index_type", pd.DatetimeIndex)
def timespan(x, param):
ix = x.index
# Get differences between the last timestamp and the first timestamp in seconds,
# then convert to hours.
times_seconds = (ix[-1] - ix[0]).total_seconds()
return times_seconds / float(3600)
步骤 3. 为您的功能添加自定义设置
最后,你需要将你的新自定义功能添加到提取设置中,否则在提取过程中不会使用它。为此,创建一个新的设置对象(默认情况下,tsfresh 使用 tsfresh.feature_extraction.settings.ComprehensiveFCParameters)并将你的函数作为字典的键添加。作为值,如果你的函数不需要参数,则使用 None,或者使用你想要使用的参数列表(作为字典)。
settings = ComprehensiveFCParameters()
settings[f] = [{"n": 1}, {"n": 2}]
之后,确保在调用 extract_features 时传递你新创建的设置。
步骤 4. 发起拉取请求
如果你能将自定义功能贡献给 tsfresh,我们将非常高兴。
为此,将您的功能添加到 feature_calculators.py 文件中,并将您的功能(作为名称)与安全的默认参数一起附加到 tsfresh.feature_extraction.settings.ComprehensiveFCParameters 构造函数内的 name_to_param 字典中:
name_to_param.update({
# here are the existing settings
...
# Now the settings of your feature calculator
"your_feature_calculator" = [{"p1": x, "p2": y, ...} for x,y in ...],
})
确保不同的特征提取设置(例如 tsfresh.feature_extraction.settings.EfficientFCParameters、tsfresh.feature_extraction.settings.MinimalFCParameters 或 tsfresh.feature_extraction.settings.ComprehensiveFCParameters)包含不同的特征计算器集合。你可以通过为你的函数赋予“minimal”或“high_comp_cost”等属性来控制哪个特征提取设置对象将包含你的新特征计算器。更多信息请参见 tsfresh.feature_extraction.settings 中的类。
之后,添加一些测试并将拉取请求提交到我们的 github 仓库。我们很乐意接受部分实现的特征计算器,我们可以一起完成它。