[ML]tsfresh:时序数据特征自动提取工具

tsfresh是什么

tsfresh 是一个用于时间序列特征生成的python包。使用tsfresh可以自动计算出大量的时间序列特征,tsfresh还内置有特征筛选算法可以挑选出和任务有关的特征。提取的特征可用于描述时间序列,这些特征可以用于下游的时间序列任务,如股票价格预测、天气预测、景点人流预测、时尚商品销量预测、商品推荐系统等。

tfresh的特点:

  1. 安装方便,使用简单;
  2. 内置数百个在各个领域验证有效的特征生成函数可调用;
  3. 支持设置目标进行特征过滤;
  4. 支持自定义新的特征函数;
  5. 支持对滚动时间序列提取特征;
  6. 支持使用Dask在处理大样本数据;
  7. 支持进行并行化加速;
  8. 可以和scikit-learn进行结合使用。
图片

如何使用tsfresh

安装tsfresh:

# pip安装

nbsp;pip install -U tsfresh
# conda安装

nbsp;conda install -c conda-forge tsfresh

使用tsfresh进行自动特征提取:

from tsfresh import extract_relevant_features
from tsfresh.examples.robot_execution_failures import load_robot_execution_failures

# 加载示例数据
timeseries, y = load_robot_execution_failures()
# 自动进行相关特征提取
features = extract_relevant_features(timeseries, y, column_id="id", column_sort="time")

tsfresh支持与scikit-learn 兼容,可以合并到现有的机器学习管道中:

from sklearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestClassifier
from tsfresh.examples import load_robot_execution_failures
from tsfresh.transformers import RelevantFeatureAugmenter
import pandas as pd

# 下载数据
from tsfresh.examples.robot_execution_failures import download_robot_execution_failures
download_robot_execution_failures()

#
pipeline = Pipeline([
    ('augmenter', RelevantFeatureAugmenter(column_id='id', column_sort='time')),
    ('classifier', RandomForestClassifier()),
])

df_ts, y = load_robot_execution_failures()
X = pd.DataFrame(index=y.index)

pipeline.set_params(augmenter__timeseries_container=df_ts)
pipeline.fit(X, y)

tsfresh支持自时间序列滚动预测,这在金融等任务上非常有用,通常使用的因子也是滚动计算的。
图片

from tsfresh import extract_features
from tsfresh.utilities.dataframe_functions import roll_time_series

df_rolled = roll_time_series(df, column_id="id", column_sort="time")
df_features = extract_features(df_rolled, column_id="id", column_sort="time")

自定义特征生成函数,tsfresh支持用户自定义特征生成函数,用户可以领域知识构建符合业务的特征生成函数以获得更有效的特征。

from tsfresh.feature_extraction.feature_calculators import set_property

@set_property("fctype", "simple"")
def your_feature_calculator(x, p1, p2, ...):
    f = x * p1 + p2
    return f

数据科学家通常将大部分时间花在构建新特征上,tsfresh可以通过自动特征提取功能来减少特征挖掘的时间,让我们有更多的时间使用挖掘出来的特征构建更好的模型。

发布者:股市刺客,转载请注明出处:https://www.95sca.cn/archives/111086
站内所有文章皆来自网络转载或读者投稿,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。敬请谅解!

(0)
股市刺客的头像股市刺客
上一篇 1天前
下一篇 1天前

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注