Auto-TS 是 AutoML 的一部分,它将自动化机器学习管道的一些组件。这自动化库有助于非专家训练基本的机器学习模型,而无需在该领域有太多知识。在本文中,小编和你一起学习如何使用 Auto-TS 库自动执行时间序列预测模型。

什么是自动 TS?

它是一个开源 Python 库,主要用于自动化时间序列预测。它将使用一行代码自动训练多个时间序列模型,这将帮助我们为我们的问题陈述选择最好的模型。

在 python 开源库 Auto-TS 中,auto-ts.Auto_TimeSeries() 使用训练数据调用的主要函数。然后我们可以选择想要的模型类型,例如 stats、ml 或FB prophet-based models (基于 FB 先知的模型)。我们还可以调整参数,这些参数将根据我们希望它基于的评分参数自动选择最佳模型。它将返回最佳模型和一个字典,其中包含提到的预测周期数的预测(默认值 = 2)。

Auto_timeseries 是用于时间序列数据的复杂模型构建实用程序。由于它自动化了复杂工作中涉及的许多任务,因此它假定了许多智能默认值。5.但是我们可以改变它们。Auto_Timeseries 将基于 Statsmodels ARIMA、Seasonal ARIMA 和 Scikit-Learn ML 快速构建预测模型。它将自动选择给出指定最佳分数的最佳模型。

Auto_TimeSeries 能够帮助我们使用 ARIMA、SARIMAX、VAR、可分解(趋势+季节性+残差)模型和集成机器学习模型等技术构建和选择多个时间序列模型。

Auto-TS 库的特点

  • 它使用遗传规划优化找到最佳时间序列预测模型。

  • 它训练普通模型、统计模型、机器学习模型和深度学习模型,具有所有可能的超参数配置和交叉验证。

  • 它通过学习最佳 NaN 插补和异常值去除来执行数据转换以处理杂乱的数据。

  • 选择用于模型选择的指标组合。

安装

pip install auto-ts  # 或
pip install git+git://github.com/AutoViML/Auto_TS

依赖包,如下依赖包需要提前安装

dask
scikit-learn
FB Prophet
statsmodels
pmdarima
XGBoost

导入库

from auto_ts import auto_timeseries

巨坑警告

根据上述安装步骤安装成功后,很大概率会出现这样的错误:

Running setup.py clean for fbprophet
Failed to build fbprophet
Installing collected packages: fbprophetRunning setup.py install for fbprophet ... error......from pystan import StanModel
ModuleNotFoundError: No module named 'pystan'

这个时候你会装pystan:pip install pystan 。安装完成后,还是会出现上述报错。如果你也出现了如上情况,不要慌,云朵君已经帮你踩过坑了。

参考解决方案:(Mac/anaconda)

1. 安装 Ephem:

conda install -c anaconda ephem

2. 安装 Pystan:

conda install -c conda-forge pystan

3. 安装 Fbprophet:

(这个会花费4小时+)

conda install -c conda-forge fbprophet

4. 最后安装:

pip install prophet
pip install fbprophet

5. 最后直到出现:

Successfully installed cmdstanpy-0.9.5 fbprophet-0.7.1 holidays-0.13

如果上述还不行,你先尝试重启anaconda,如果还不行,则需要先安装:

conda install gcc

再上述步骤走一遍。

上述过程可能要花费1天时间!!

最后尝试导入,成功!

from auto_ts import auto_timeseries
Imported auto_timeseries version:0.0.65. Call by using:
model = auto_timeseries(score_type='rmse',
time_interval='M',
non_seasonal_pdq=None,
seasonality=False,
seasonal_period=12,
model_type=['best'],
verbose=2,
dask_xgboost_flag=0)
model.fit(traindata,
ts_column,target)
model.predict(testdata, model='best')

auto_timeseries 中可用的参数

model = auto_timeseries(
score_type='rmse',
time_interval='Month',
non_seasonal_pdq=None,
seasonity=False,
season_period=12,
model_type=['Prophet'],verbose=2)

可以调整参数并分析模型性能的变化。有关参数的更多详细信息参考auto-ts文档[1]

使用的数据集

本文使用了从 Kaggle 下载的 2006 年 1 月至 2018 年 1 月的亚马逊股票价格[2]数据集。该库仅提供训练时间序列预测模型。数据集应该有一个时间或日期格式列。

最初,使用时间/日期列加载时间序列数据集:

df = pd.read_csv("Amazon_Stock_Price.csv", usecols=['Date', 'Close'])
df['Date'] = pd.to_datetime(df['Date'])
df = df.sort_values('Date')

现在,将整个数据拆分为训练数据和测试数据:

train_df = df.iloc[:2800]
test_df = df.iloc[2800:]

现在,我们将可视化拆分训练测试:

train_df.Close.plot(figsize=(15,8), title= 'AMZN Stock Price', fontsize=14, label='Train')
test_df.Close.plot(figsize=(15,8), title= 'AMZN Stock Price', fontsize=14, label='Test')

现在,让我们初始化 Auto-TS 模型对象,并拟合训练数据:

model = auto_timeseries(forecast_period=219, score_type='rmse', time_interval='D', model_type='best')
model.fit(traindata= train_df,ts_column="Date",target="Close")

现在让我们比较不同模型的准确率:

model.get_leaderboard()
model.plot_cv_scores()

得到如下结果:

Start of Fit.....

Target variable given as = Close

Start of loading of data.....

Inputs: ts_column = Date, sep = ,, target = ['Close']

Using given input: pandas dataframe...

Date column exists in given train data...

train data shape = (2800, 1)

Alert: Could not detect strf_time_format of Date. Provide strf_time format during "setup" for better results.

Running Augmented Dickey-Fuller test with paramters:

maxlag: 31 regression: c autolag: BIC

Data is stationary after one differencing

There is 1 differencing needed in this datasets for VAR model

No time series plot since verbose = 0. Continuing

Time Interval is given as D

Correct Time interval given as a valid Pandas date-range frequency...

WARNING: Running best models will take time... Be Patient...

==================================================

Building Prophet Model

==================================================

Running Facebook Prophet Model...

Starting Prophet Fit

No seasonality assumed since seasonality flag is set to False

Starting Prophet Cross Validation

Max. iterations using expanding window cross validation = 5

Fold Number: 1 --> Train Shape: 1705 Test Shape: 219

RMSE = 30.01

Std Deviation of actuals = 19.52

Normalized RMSE (as pct of std dev) = 154%

Cross Validation window: 1 completed

Fold Number: 2 --> Train Shape: 1924 Test Shape: 219

RMSE = 45.33

Std Deviation of actuals = 34.21

Normalized RMSE (as pct of std dev) = 132%

Cross Validation window: 2 completed

Fold Number: 3 --> Train Shape: 2143 Test Shape: 219

RMSE = 65.61

Std Deviation of actuals = 39.85

Normalized RMSE (as pct of std dev) = 165%

Cross Validation window: 3 completed

Fold Number: 4 --> Train Shape: 2362 Test Shape: 219

RMSE = 178.53

Std Deviation of actuals = 75.28

Normalized RMSE (as pct of std dev) = 237%

Cross Validation window: 4 completed

Fold Number: 5 --> Train Shape: 2581 Test Shape: 219

RMSE = 148.18

Std Deviation of actuals = 57.62

Normalized RMSE (as pct of std dev) = 257%

Cross Validation window: 5 completed

-------------------------------------------

Model Cross Validation Results:

-------------------------------------------

MAE (Mean Absolute Error = 85.20

MSE (Mean Squared Error = 12218.34

MAPE (Mean Absolute Percent Error) = 17%

RMSE (Root Mean Squared Error) = 110.5366

Normalized RMSE (MinMax) = 18%

Normalized RMSE (as Std Dev of Actuals)= 60%

Time Taken = 13 seconds

End of Prophet Fit

==================================================

Building Auto SARIMAX Model

==================================================

Running Auto SARIMAX Model...

Using smaller parameters for larger dataset with greater than 1000 samples

Using smaller parameters for larger dataset with greater than 1000 samples

Using smaller parameters for larger dataset with greater than 1000 samples

Using smaller parameters for larger dataset with greater than 1000 samples

Using smaller parameters for larger dataset with greater than 1000 samples

SARIMAX RMSE (all folds): 73.9230

SARIMAX Norm RMSE (all folds): 35%

-------------------------------------------

Model Cross Validation Results:

-------------------------------------------

MAE (Mean Absolute Error = 64.24

MSE (Mean Squared Error = 7962.95

MAPE (Mean Absolute Percent Error) = 12%

RMSE (Root Mean Squared Error) = 89.2354

Normalized RMSE (MinMax) = 14%

Normalized RMSE (as Std Dev of Actuals)= 48%

Using smaller parameters for larger dataset with greater than 1000 samples

Refitting data with previously found best parameters

Best aic metric = 18805.2

SARIMAX Results

==============================================================================

Dep. Variable:                  Close   No. Observations:                 2800

Model:               SARIMAX(2, 2, 0)   Log Likelihood               -9397.587

Date:                Mon, 28 Feb 2022   AIC                          18805.174

Time:                        19:45:31   BIC                          18834.854

Sample:                             0   HQIC                         18815.888

- 2800

Covariance Type:                  opg

==============================================================================

coef    std err          z      P>|z|      [0.025      0.975]

------------------------------------------------------------------------------

intercept     -0.0033      0.557     -0.006      0.995      -1.094       1.088

drift       3.618e-06      0.000      0.015      0.988      -0.000       0.000

ar.L1         -0.6405      0.008    -79.601      0.000      -0.656      -0.625

ar.L2         -0.2996      0.009    -32.618      0.000      -0.318      -0.282

sigma2        48.6323      0.456    106.589      0.000      47.738      49.527

===================================================================================

Ljung-Box (L1) (Q):                  14.84   Jarque-Bera (JB):             28231.48

Prob(Q):                              0.00   Prob(JB):                         0.00

Heteroskedasticity (H):              19.43   Skew:                             0.56

Prob(H) (two-sided):                  0.00   Kurtosis:                        18.53

===================================================================================

Warnings:

[1] Covariance matrix calculated using the outer product of gradients (complex-step).

===============================================

Skipping VAR Model since dataset is > 1000 rows and it will take too long

===============================================

==================================================

Building ML Model

==================================================

Creating 2 lagged variables for Machine Learning model...

You have set lag = 3 in auto_timeseries setup to feed prior targets. You cannot set lags > 10 ...

### Be careful setting dask_xgboost_flag to True since dask is unstable and doesn't work sometime's ###

########### Single-Label Regression Model Tuning and Training Started ####

Fitting ML model

11 variables used in training ML model = ['Close(t-1)', 'Date_hour', 'Date_minute', 'Date_dayofweek', 'Date_quarter', 'Date_month', 'Date_year', 'Date_dayofyear', 'Date_dayofmonth', 'Date_weekofyear', 'Date_weekend']

Running Cross Validation using XGBoost model..

Max. iterations using expanding window cross validation = 2

train fold shape (2519, 11), test fold shape = (280, 11)

### Number of booster rounds = 250 for XGBoost which can be set during setup ####

Hyper Param Tuning XGBoost with CPU parameters. This will take time. Please be patient...

Cross-validated Score = 31.896 in num rounds = 249

Time taken for Hyper Param tuning of XGBoost (in minutes) = 0.0

Top 10 features:

['Date_year', 'Close(t-1)', 'Date_quarter', 'Date_month', 'Date_weekofyear', 'Date_dayofyear', 'Date_dayofmonth', 'Date_dayofweek']

Time taken for training XGBoost on entire train data (in minutes) = 0.0

Returning the following:

Model = <xgboost.core.Booster object at 0x7feb8dd30070>

Scaler = Pipeline(steps=[('columntransformer',

ColumnTransformer(transformers=[('simpleimputer',

SimpleImputer(),

['Close(t-1)', 'Date_hour',

'Date_minute',

'Date_dayofweek',

'Date_quarter', 'Date_month',

'Date_year',

'Date_dayofyear',

'Date_dayofmonth',

'Date_weekofyear',

'Date_weekend'])])),

('maxabsscaler', MaxAbsScaler())])

(3) sample predictions:[359.8374  356.59747 355.447  ]

XGBoost model tuning completed

Target = Close...CV results:

RMSE = 246.63

Std Deviation of actuals = 94.60

Normalized RMSE (as pct of std dev) = 261%

Fitting model on entire train set. Please be patient...

Time taken to train model (in seconds) = 0

Best Model is: auto_SARIMAX

Best Model (Mean CV) Score: 73.92

--------------------------------------------------

Total time taken: 52 seconds.

--------------------------------------------------

Leaderboard with best model on top of list:

name        rmse

1  auto_SARIMAX   73.922971

0       Prophet   93.532440

2            ML  246.630613

现在我们在测试数据上测试我们的模型:

future_predictions = model.predict(testdata=219)
# 或
model.predict(testdata=test_df.Close)

使用预测周期=219作为auto_SARIMAX模型的输入进行预测:

future_predictions

可视化看下future_predictions是什么样子:

最后,可视化测试数据值和预测:

pred_df = pd.concat([test_df,future_predictions],axis=1)
ax.plot('Date','Close','b',data=pred_df,label='Test')
ax.plot('Date','yhat','r',data=pred_df,label='Predicitions')

auto_timeseries 中可用的参数:

model = auto_timeseries( score_type='rmse',time_interval='Month',non_seasonal_pdq=None, seasonity=False,  season_period=12,model_type=['Prophet'],verbose=2)

model.fit() 中可用的参数:

model.fit(traindata=train_data,ts_column=ts_column,target=target,cv=5, sep="," )

model.predict() 中可用的参数:

model = model.predict(testdata = '可以是数据框或代表预测周期的整数';  model = 'best', '或代表训练模型的任何其他字符串')

可以使用所有这些参数并分析我们模型的性能,然后可以为我们的问题陈述选择最合适的模型。可以查看auto-ts文档[3]详细检查所有这些参数。

写在最后

在本文中,讨论了如何在一行 Python 代码中自动化时间序列模型。Auto-TS 对数据进行预处理,因为它从数据中删除异常值并通过学习最佳 NaN 插补来处理混乱的数据。

通过初始化 Auto-TS 对象并拟合训练数据,它将自动训练多个时间序列模型,例如 ARIMA、SARIMAX、FB Prophet、VAR,并得出性能最佳的模型。模型的结果跟数据集的大小有一定的关系。如果我们尝试增加数据集的大小,结果应该会有所改善。

参考资料

[1]

auto-ts文档: https://pypi.org/project/auto-ts/

[2]

亚马逊股票价格: https://www.kaggle.com/szrlee/stock-time-series-20050101-to-20171231?select=AMZN_2006-01-01_to_2018-01-01.csv

[3]

auto-ts文档: https://pypi.org/project/auto-ts/

往期精彩回顾适合初学者入门人工智能的路线及资料下载(图文+视频)机器学习入门系列下载中国大学慕课《机器学习》(黄海广主讲)机器学习及深度学习笔记等资料打印《统计学习方法》的代码复现专辑
AI基础下载机器学习交流qq群955171419,加入微信群请扫码:

【时间序列】使用 Auto-TS 自动化时间序列预测相关推荐

  1. 使用 FEDOT 自动化时间序列预测

    大多数现代开源AutoML框架并没有广泛地涵盖时间序列预测任务.本文中我们将深入地研究AutoML框架之一FEDOT,它可以自动化时间序列预测的机器学习管道设计.因此,我们将通过时间序列预测的现实世界 ...

  2. 用python做时间序列预测十:时间序列实践-航司乘客数预测

    本文以航司乘客数预测的例子来组织相关时间序列预测的代码,通过了解本文中的代码,当遇到其它场景的时间序列预测亦可套用. 航司乘客数序列 预测步骤 # 加载时间序列数据 _ts = load_data() ...

  3. 生猪价格matlab,基于时间序列的BP神经网络猪肉价格预测

    科技创新 2m6钎第20期I科技创新与应用 基于时间序列的BP神经网络猪肉价格预测 张津张瑞斌 (成都理工大学管理科学学院,四川成都610059) 摘要:猪肉价格是不稳定的,起伏变化的,猪肉价格的预测 ...

  4. python 时间序列prophet 模型分析_时间序列预测模型Prophet[Facebook]

    Forecasting at Scale Abstract 预测是十分常见的数据科学任务,可用于企业的容量规划,目标设定,异常检测等.当时间序列多种多样并且缺少专家经验时,时间序列预测就变得尤其困难. ...

  5. 【时间序列】KDD 2021丨时间序列相关研究论文汇总

    ACM SIGKDD(Conference on Knowledge Discovery and Data Mining, KDD)是全球最大规模的国际数据科学会议,将展示知识发现和数据挖掘方面的最新 ...

  6. 时间序列教程 三、对时间序列数据进行平滑处理

    一.本节目标 了解数据平滑的必要性. 列举常用的数据平滑技术. 解释常见的数据平滑技术是如何工作的. 使用Python平滑时间序列数据. 二.数据平滑及其重要性 1.平滑的概念 平滑是一个从数据中提取 ...

  7. Python使用matplotlib可视化时间序列数据、并为时间序列曲线添加误差带、使用95%置信区间(Time Series Error Bands with confidence interval

    Python使用matplotlib可视化时间序列数据.并为时间序列曲线添加误差带.使用95%置信区间(Time Series with Error Bands with confidence int ...

  8. matplotlib可视化时间序列数据、并高亮时间序列中的指定区域(Highlight a Region of Time-Series Plot with Matplotlib)

    matplotlib可视化时间序列数据.并高亮时间序列中的指定区域(Highlight a Region of Time-Series Plot with Matplotlib) 目录

  9. 时间序列、时间序列分析、时间序列效应分解、平稳时间序列、AIC和BIC

    时间序列.时间序列分析.时间序列效应分解.平稳时间序列.AIC和BIC 目录 时间序列.时间序列分析.时间序列效应分解.平稳时间序列.AIC和BIC

最新文章

  1. Cygwin获取root权限
  2. Nginx的配置文件nginx.conf详解
  3. 【python初识】列表与函数模块
  4. 同一台电脑上使用两个 github 账号
  5. PHPstorm中使用数组短语法[],出现红色波浪
  6. spark有什么作用_Spark 101:它是什么,它做什么以及为什么起作用
  7. 【Java基础】 JQuery的常用操作
  8. 不同程序语言的注释和变量要求
  9. leetcode719:直线上的第k近点对
  10. python语言中的模块包括_python中常用的模块的总结
  11. 福利!H3CNE认证网络工程师GB0-191 考试试题库超详细解析-02。持续更新~~
  12. OS知识点汇总(考研用)——第二章:进程管理(下)
  13. 【实验】虚拟驱动模拟实验
  14. python 从键盘输入若干个整数,当输入“-1““时输入结束,请统计最大值、最小值和平均数
  15. 欧几里得距离和曼哈顿距离
  16. python中#!含义
  17. ML之PDP:基于titanic泰坦尼克是否获救二分类预测数据集利用PDP部分依赖图对RF随机森林实现模型可解释性案例
  18. Android9.0 本地时区和本地时间的自动更新机制
  19. 怎么配置java环境
  20. Springboot 系列(十)使用 Spring data jpa 访问数据库

热门文章

  1. 【BZOJ2819】Nim 树状数组+LCA
  2. SQL 基础知识梳理1
  3. 运用java集合Collections对List进行max和min操作
  4. git 提交各种情况下的处理方式
  5. 百度云观测优化建议解决方案:未设置max-age或expires
  6. 用DateTime.ToString(string format)输出不同格式的日期
  7. 手把手教你使用VS制作winform安装包
  8. 数据结构50:二分查找法(折半查找法)
  9. SpringBoot之Servlet、Filter、Listener配置
  10. POJ 3436 ACM Computer Factory(最大流+路径输出)