如何添加自定义特征
如果您想从时间序列中提取自定义特征,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 仓库提交拉取请求。我们乐于接受部分实现的特征计算器,我们可以共同完成。