时间序列数据特征提取TsFresh--入门简介【附源码+数据】

时间序列数据特征提取TsFresh–入门简介

简介

tsfresh用于从时间序列中提取特征。本篇主要介绍时间序列时间特征的拓展和过滤。编程环境jupyter notebook

源码和数据地址

链接:https://pan.baidu.com/s/1VjHgdcIx9WY3VigHMkBCKw
提取码:gqs1

TsFresh安装

pip install tsfresh

代码部分说明

数据下载

from tsfresh.examples.robot_execution_failures import download_robot_execution_failures, load_robot_execution_failures
download_robot_execution_failures()
timeseries, y = load_robot_execution_failures()

数据展示

timeseries.describe()

时间序列数据特征提取TsFresh--入门简介【附源码+数据】_第1张图片

y.head()

时间序列数据特征提取TsFresh--入门简介【附源码+数据】_第2张图片

从文件读取数据

import pandas as pd
import numpy as np
timeseries_csv = pd.read_csv('load_robot_execution_failures_timeseries.csv')
timeseries_csv.describe()

时间序列数据特征提取TsFresh--入门简介【附源码+数据】_第3张图片

y_csv = pd.read_csv('load_robot_execution_failures_y.csv',usecols = ['index','value'])
y_csv= pd.Series(np.array(y_csv['value']).tolist(),index =np.array(y_csv['index']).tolist())
y_csv.head()

时间序列数据特征提取TsFresh--入门简介【附源码+数据】_第4张图片

特征拓展

1、默认参数

from tsfresh.feature_extraction import extract_features
extracted_features_0 = extract_features(timeseries, column_id="id", column_sort="time")
extracted_features_0.shape

在这里插入图片描述

2、ComprehensiveFCParameters参数

from tsfresh.feature_extraction import extract_features, ComprehensiveFCParameters
settings = ComprehensiveFCParameters()
extracted_features_1 = extract_features(timeseries, column_id="id", column_sort="time",default_fc_parameters=settings)
extracted_features_1.shape

在这里插入图片描述

3、EfficientFCParameters参数

from tsfresh.feature_extraction import extract_features, EfficientFCParameters
settings = EfficientFCParameters()
extracted_features_2 = extract_features(timeseries, column_id="id", column_sort="time",default_fc_parameters=settings)
extracted_features_2.shape

在这里插入图片描述

4、MinimalFCParameters参数

from tsfresh.feature_extraction import extract_features, MinimalFCParameters
settings = MinimalFCParameters()
extracted_features_3 = extract_features(timeseries, column_id="id", column_sort="time",default_fc_parameters=settings)
extracted_features_3.shape

在这里插入图片描述

特征过滤

1、默认参数

from tsfresh import select_features
from tsfresh.utilities.dataframe_functions import impute
impute(extracted_features_0)
features_filtered_0 = select_features(extracted_features_0, y) #特征选择
features_filtered_0.shape

时间序列数据特征提取TsFresh--入门简介【附源码+数据】_第5张图片

2、ComprehensiveFCParameters参数

from tsfresh import select_features
from tsfresh.utilities.dataframe_functions import impute
impute(extracted_features_1)
features_filtered_1 = select_features(extracted_features_1, y) #特征选择
features_filtered_1.shape

时间序列数据特征提取TsFresh--入门简介【附源码+数据】_第6张图片

3、EfficientFCParameters参数

from tsfresh import select_features
from tsfresh.utilities.dataframe_functions import impute
impute(extracted_features_2)
features_filtered_2 = select_features(extracted_features_2, y) #特征选择
features_filtered_2.shape

时间序列数据特征提取TsFresh--入门简介【附源码+数据】_第7张图片

4、MinimalFCParameters参数

from tsfresh import select_features
from tsfresh.utilities.dataframe_functions import impute
impute(extracted_features_3)
features_filtered_3 = select_features(extracted_features_3, y) #特征选择
features_filtered_3.shape

在这里插入图片描述

特征拓展和过滤同时进行

1、默认参数

from tsfresh import extract_relevant_features
features_filtered_direct_0 = extract_relevant_features(timeseries,y, column_id="id", column_sort="time")
features_filtered_direct_0.shape

在这里插入图片描述

2、ComprehensiveFCParameters参数

from tsfresh.feature_extraction import ComprehensiveFCParameters
from tsfresh import extract_relevant_features
settings = ComprehensiveFCParameters()
features_filtered_direct_1 = extract_relevant_features(timeseries,y, column_id="id", column_sort="time",default_fc_parameters=settings)
features_filtered_direct_1.shape

在这里插入图片描述

3、EfficientFCParameters参数

from tsfresh.feature_extraction import EfficientFCParameters
from tsfresh import extract_relevant_features
settings = EfficientFCParameters()
features_filtered_direct_2 = extract_relevant_features(timeseries,y, column_id="id", column_sort="time",default_fc_parameters=settings)
features_filtered_direct_2.shape

在这里插入图片描述

4、MinimalFCParameters参数

from tsfresh.feature_extraction import MinimalFCParameters
from tsfresh import extract_relevant_features
settings = MinimalFCParameters()
features_filtered_direct_3 = extract_relevant_features(timeseries,y, column_id="id", column_sort="time",default_fc_parameters=settings)
features_filtered_direct_3.shape

在这里插入图片描述

结论

通过拓展+滤波,和直接一次性滤波效果一致。

参考

【1】 tsfresh官网
【2】 tsfresh GitHub
【3】时序数据处理工具-时间序列数据特征提取TsFresh

你可能感兴趣的:(python,机器学习,人工智能)