scikit-learn 转换器
tsfresh 包含了三个与 scikit-learn 兼容的转换器,这些转换器允许你轻松地将时间序列的特征提取和特征选择集成到现有的机器学习管道中。
scikit-learn 的管道允许你组合多个预处理步骤,这些步骤将按顺序执行,因此可以在设置不同参数的情况下一起进行交叉验证(有关 scikit-learn 管道的更多详细信息,请查看官方文档 [1])。我们的 tsfresh 转换器允许你在这些预处理序列中提取和过滤时间序列特征。
tsfresh 中的前两个估计器是 FeatureAugmenter,它提取特征,以及 FeatureSelector,它执行特征选择算法。最好在一个步骤中结合特征的提取和过滤,以避免不必要的特征计算。因此,RelevantFeatureAugmenter 在一个步骤中结合了特征的提取和过滤。
示例
在下面的示例中,您可以看到我们如何将 tsfresh 的 RelevantFeatureAugmenter 和 RandomForestClassifier 组合到一个单一的管道中。这个管道可以一步完成我们的转换器和分类器的拟合。
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
# Download dataset
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)
RelevantFeatureAugmenter 的参数对应于顶层便捷函数 extract_relevant_features() 的参数。在上面的例子中,我们只设置了两个列名 column_id='id', column_sort='time' (有关这些参数的更多详细信息,请参见 数据格式)。
因为我们在调用 sklearn.pipeline.Pipeline 的 fit 或 transform 方法时,不能直接将时间序列容器作为参数传递给增强步骤,所以我们必须通过调用 pipeline.set_params(augmenter__timeseries_container=df_ts) 手动设置它。一般来说,你可以通过调用管道的 set_params() 方法或转换器的 set_timeseries_container() 方法来更改从中提取特征的时间序列容器。
更多示例,请访问 tsfresh github 仓库的 notebooks 文件夹中的 Jupyter Notebook 02 sklearn Pipeline.ipynb。