时间序列数据分析方法 - bettermorn/IAICourse GitHub Wiki
1.数据源
参考数据集
- Monash, UEA & UCR Time Series Extrinsic Regression Repository http://tseregression.org/
- UCI数据集UCI Machine Learning Repository https://archive.ics.uci.edu/ml/datasets.php
- UCR数据集UCR Time Series Classification Archive https://www.cs.ucr.edu/~eamonn/time_series_data_2018/
- UEA & UCR Time Series Classification Repository http://www.timeseriesclassification.com/Downloads
TS格式
格式说明
采用常见时间序列数据格式 .ts 参考Loading and working with data in sktime https://www.sktime.org/en/latest/examples/loading_data.html#ts_files 表示如下
@problemname BenzeneConcentration
@timestamps true
@missing true
@univariate false
@dimension dim#no
@equallength true
@serieslength series#no
@targetlabel true
@data
(t0,f1),(t1,f1),....(tseries#no-1,f1):(t0,fdim#no-1),(t1,fdim#no-1),....(tseries#no-1,fdim#no-1):(t0,ftarget),(t1,ftarget),....(tseries#no-1,ftarget)
Note:the class label for an instance should be specified in the last dimension and @classLabel should be set to true in the header information and be followed by the set of possible class values.
- instances:文件中的每一行代表不同的序列,An instance may contain 1 to many dimensions, where instances are line-delimited and dimensions within an instance are colon (:) delimited.
- dimension: 自变量维度
- series:每一个序列中的采样时间点数
- targetlabel: 对回归问题,对分类问题,使用classlabel
数据转换方式
数据准备方法
- 参考https://colab.research.google.com/github/timeseriesAI/tsai/blob/master/tutorial_nbs/00c_Time_Series_data_preparation.ipynb Time Series data preparation: this will show how you can do classify both univariate or multivariate time series.Time Series data preparation: this will show how you can do classify both univariate or multivariate time series.
- 使用TSAI库,数据的3个维度
- number of samples:可以根据具体情况区分多个样本,也可以对1个样本进行 SlidingWindow,分成多个样本
- number of features (aka variables, dimensions, channels):自变量,维度
- number of steps (or length, time steps, sequence steps):一个序列中的多个步骤
2.数据分析步骤
参考https://timeseriesai.github.io/tsai/
# Multivariate regression ensemble with sklearn-type API
from sklearn.metrics import mean_squared_error
dsid = 'AppliancesEnergy'
X_train, y_train, X_valid, y_valid = get_Monash_regression_data(dsid)
rmse_scorer = make_scorer(mean_squared_error, greater_is_better=False)
model = MiniRocketVotingRegressor(n_estimators=5, scoring=rmse_scorer)
timer.start(False)
model.fit(X_train, y_train)
t = timer.stop()
y_pred = model.predict(X_valid)
rmse = mean_squared_error(y_valid, y_pred, squared=False)
print(f'valid rmse : {rmse:.5f} time: {t}')
步骤如下:
- 确定数据集名称
- 准备训练数据和测试数据
- 确定评价指标
- 选择模型(例如回归模型,确定模型参数、相关评价指标)
- 启动定时器
- 用模型训练
- 停止计时
- 计算模型评价指标
- 打印模型指标
以TST为例,使用学习器训练如下,TST (Time Series Transformer) seems like a great addition to the world of time series models.The model trains very smoothly and overfitting can be reduced/ eliminated by using dropout.Also, TST is about 10% faster to train that InceptionTime. Here's all the code you need to train a transformer model with tsai: 参考代码 https://github.com/timeseriesAI/tsai/blob/main/tutorial_nbs/07_Time_Series_Classification_with_Transformers.ipynb
X, y, splits = get_UCR_data('FaceDetection', return_split=False)
tfms = [None, [Categorize()]]
dsets = TSDatasets(X, y, tfms=tfms, splits=splits)
dls = TSDataLoaders.from_dsets(dsets.train, dsets.valid, bs=64, batch_tfms=TSStandardize(by_var=True))
model = TST(dls.vars, dls.c, dls.len, res_dropout=0.3, fc_dropout=0.9)
learn = Learner(dls, model, loss_func=LabelSmoothingCrossEntropyFlat(),
metrics=[RocAucBinary(), accuracy], cbs=ShowGraphCallback2())
learn.fit_one_cycle(100, 1e-4)
注: Learner来自fastai,本库使用的代码中用到的 ts_learner为fasti中的扩展方法,可查看 https://timeseriesai.github.io/tsai/learner.html#ts_learner
3. 模型选择
3.1 ROCKET(RandOm Convolutional KErnel Transform)
- 论文 https://arxiv.org/pdf/1910.13051.pdf ROCKET: Exceptionally fast and accurate time series classification using random convolutional kernels
- 论文 https://arxiv.org/pdf/2012.08791.pdf MINIROCKET: A Very Fast (Almost) Deterministic Transform for Time Series Classification
- 论文摘要:ROCKET 通过使用随机卷积核转换输入时间序列并使用转换后的特征来训练线性分类器,从而以大多数现有方法的一小部分计算费用实现了最先进的准确性。我们将 ROCKET 重新构造为一种新方法 MINIROCKET,使其在更大的数据集上的速度提高了 75 倍,并使其几乎是确定性的(并且可选地,通过额外的计算费用,完全确定性),同时保持基本相同的准确性。使用这种方法,可以在不到 10 分钟的时间内对来自 UCR 档案的所有 109 个数据集的分类器进行训练和测试,以达到最先进的准确度。 MINIROCKET 比具有可比精度的任何其他方法(包括 ROCKET)快得多,并且比任何其他计算成本甚至大致相似的方法都要准确得多。
- 代码文档 https://timeseriesai.github.io/tsai/models.ROCKET.html
- 具体方法:ROCKET 将大量固定的、不可训练的、独立的卷积应用于时间序列。然后从每个卷积输出(一种池化形式)中提取许多特征,每个样本通常生成 10000 个特征。 (这些功能只是浮点数。)特征被存储以便它们可以被多次使用。然后它学习一个简单的线性头部,根据其特征预测每个时间序列样本。典型的 PyTorch 头可能基于线性层。当训练样本数量较少时,经常使用sklearn的RidgeClassifier。通过实验选择了卷积的固定权重和池化方法,以有效预测广泛的现实世界时间序列。最初的 ROCKET 方法使用了一系列固定卷积,权重根据随机分布选择。基于从 ROCKET 吸取的经验教训,MiniRocket 将卷积细化为特定的预定义集合,该集合被证明至少与 ROCKET 一样有效。它的计算速度也比原来的 ROCKET 快得多。实际上,论文作者“建议现在应该考虑使用 MiniRocket 作为 Rocket 的默认变体。”MiniROCKET 是在 Python 中使用 numba 加速和特定于算法的数学加速实现的。它运行速度非常快,并行使用 CPU 内核。 MiniRocket 的 2 个实现:具有类似 sklearn 的 API 的 cpu 版本(可用于小数据集 - <10k 样本),以及MiniRocket 的 PyTorch 实现,针对 GPU 进行了优化。它的运行速度比 CPU 版本快(3-25 倍,具体取决于您的 GPU),并为进一步实验提供了一定的灵活性。
- MINIRocket效果不好的原因在于:只有卷积特征,没有序列特征。
3.2 TST Time Series Transformer
- 论文 Zerveas, G., Jayaraman, S., Patel, D., Bhamidipaty, A., & Eickhoff, C. (2020). A Transformer-based Framework for Multivariate Time Series Representation Learning. arXiv preprint arXiv:2010.02803v2
- 论文摘要:首次提出了一个基于变换器的框架,用于多元时间序列的无监督表示学习。预训练模型可用于下游任务,例如回归和分类、预测和缺失值插补。通过在多个用于多元时间序列回归和分类的基准数据集上评估我们的模型,我们表明我们的建模方法不仅代表了迄今为止对多元时间序列采用无监督学习的最成功的方法,而且还超越了当前的状态——监督方法的最先进性能;即使训练样本的数量非常有限,它也会这样做,同时提供计算效率。最后,我们证明了我们的 Transformer 模型的无监督预训练比完全监督学习提供了显着的性能优势,即使没有利用额外的未标记数据,即通过无监督目标重用相同的数据样本。
- 代码文档 https://timeseriesai.github.io/tsai/models.TST.html
3.3 Inceptiontime
- 论文 https://arxiv.org/abs/1909.04939 InceptionTime: Finding AlexNet for Time Series Classification
- 论文摘要:本文将深度学习置于时间序列分类 (TSC) 研究的前沿。 TSC 是机器学习领域,其任务是对时间序列进行分类(或标记)。该领域过去几十年的工作在分类器的准确性方面取得了重大进展,目前最先进的技术由 HIVE-COTE 算法代表。虽然非常准确,但 HIVE-COTE 不能应用于许多现实世界的数据集,因为它对于具有 N 个长度为 T 的时间序列的数据集的训练时间复杂度为 O(N2 * T4)。例如,它需要更多的 HIVE-COTE从一个 N = 1500 个短长度 T = 46 的时间序列的小数据集学习超过 8 天。同时深度学习因其高准确率和可扩展性而受到了极大的关注。最近的 TSC 深度学习方法具有可扩展性,但不如 HIVE-COTE 准确。我们介绍了 InceptionTime——受 Inception-v4 架构启发的深度卷积神经网络 (CNN) 模型的集合。我们的实验表明,InceptionTime 在准确性方面与 HIVE-COTE 不相上下,同时具有更高的可扩展性:它不仅可以在一小时内从 1,500 个时间序列中学习,而且在 13 小时内也可以从 800 个时间序列中学习,数量为完全超出 HIVE-COTE 范围的数据。
- 代码文档 https://timeseriesai.github.io/tsai/models.InceptionTime.html
3.4 其他模型
- RNN_FCN 参考https://timeseriesai.github.io/tsai/models.RNN_FCNPlus.html
- LSTM_FCN 参考https://timeseriesai.github.io/tsai/models.RNN_FCNPlus.html
- RNNPlus 参考https://timeseriesai.github.io/tsai/models.RNNPlus.html
- LSTMPlus 参考https://timeseriesai.github.io/tsai/models.RNNPlus.html
- GRUPlus 参考https://timeseriesai.github.io/tsai/models.RNNPlus.html
3.5 模型的batchsize(bs)和学习率选择
- https://towardsdatascience.com/implementing-a-batch-size-finder-in-fastai-how-to-get-a-4x-speedup-with-better-generalization-813d686f6bdf
- https://sgugger.github.io/how-do-you-find-a-good-learning-rate.html
3.6 参考方法和代码
-
How to Develop LSTM Models for Time Series Forecasting https://machinelearningmastery.com/how-to-develop-lstm-models-for-time-series-forecasting/
-
How to Convert a Time Series to a Supervised Learning Problem in Python https://machinelearningmastery.com/convert-time-series-supervised-learning-problem-python/ 使用滞后的观测值t-timesteps预测t,例如timesteps=1 为以下例子,其中var1为预测变量 Input:var1(t-1),var2(t-1),var3(t-1),var4(t-1),...varN(t-1) Output: var1(t) Chinese version:Keras中带LSTM的多变量时间序列预测 https://cloud.tencent.com/developer/article/1041442 在对监督学习的时间序列数据集进行处理时,创建滞后观察列和预测列是必需的。
-
Time Series Prediction with LSTM Recurrent Neural Networks in Python with Keras https://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/
-
Multivariate Multi-step Time Series Forecasting using Stacked LSTM sequence to sequence Autoencoder in Tensorflow 2.0 / Keras https://www.analyticsvidhya.com/blog/2020/10/multivariate-multi-step-time-series-forecasting-using-stacked-lstm-sequence-to-sequence-autoencoder-in-tensorflow-2-0-keras/
3.7 理论参考
来自CMU-Amazon五位学者给了《时间序列预测:理论与实践》的Tutorial报告
共379页PPT,阐述了解决大规模预测问题提供最重要的方法和工具的简明和直观的概述。本教程的目标是为解决大规模预测问题提供最重要的方法和工具的简明和直观的概述。我们回顾了三个相关领域的研究现状: (1)时间序列的经典建模,(2)预测的现代方法,包括张量分析和深度学习。此外,我们还讨论了建立大规模预测系统的实际问题,包括数据集成、特征生成、回溯测试框架、误差跟踪和分析等。我们的重点是提供一个直观的概述方法和实际问题,我们将通过案例研究和互动材料与Jupyter笔记本说明。
- 时间序列预测经典方法 Classical Methods for Big Time Series Forecasting
- Similarity search and indexing
- DSP: Fourier and wavelets
- Linear forecasting
- Non-linear forecasting
- Tensors
- 时间序列预测现代方法 Modern Methods for Big Time Series Forecasting
- Multi-layer perceptron (feedforward neural networks)
- Recurrent neural networks (RNN)s: canonical, Sequence-to-Sequence and other - - rchitectures
- Probibilistic Forecast with NNs
- Others structures: Convolution, WaveNet, Transfomers, and all that
- Deep Probabilistic Models for Forecasting
- 时间序列预测实践 Forecasting in Practice
- Building Large Scale Forecasting Systems
- Getting start with Forecasting with
- GluonTS: A Probabilistic Time Series Library
- DeepAR on SageMaker
- Amazon Forecast
- Part 1: Classical Methods for Big Time Series Forecasting https://github.com/bettermorn/IAICourse/blob/main/refermaterials/fcst-tutorial-www-part-1%20(1).pdf
- Part 2: Modern Methods for Big Time Series Forecasting, and Forecasting in Practice 链接: https://pan.baidu.com/s/1HEpKtdZK-WJk8TgutZvxBw 提取码: rh56
4. 学习资料
- Lesson 4 - Time Series https://walkwithfastai.com/TimeSeries
- 索邦大学121页博士论文《时间序列中的无监督异常检测》 https://www.zhuanzhi.ai/vip/c199f17d65bc0a31e819bfc540f04e59