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/

推荐阅读:

我的2022届互联网校招分享

我的2021总结

浅谈算法岗和开发岗的区别

互联网校招研发薪资汇总

2022届互联网求职现状,金9银10快变成铜9铁10!!

公众号:AI蜗牛车

保持谦逊、保持自律、保持进步

发送【蜗牛】获取一份《手把手AI项目》(AI蜗牛车著)

发送【1222】获取一份不错的leetcode刷题笔记

发送【AI四大名著】获取四本经典AI电子书

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

  1. 【时间序列】使用 Auto-TS 自动化时间序列预测

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

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

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

  3. R 利用回归分析与时间序列预测北京市PM2.5

    注:代码全部在最后,数据来源UCI,链接如下: https://archive.ics.uci.edu/ml/datasets/Beijing+PM2.5+Data 摘要 现代社会科技进步,人们的生活 ...

  4. 时间序列预测任务的模型选择最全总结

    在第一部分,将了解多种时间序列的模型,如 经典的时间序列模型 监督学习模型 基于深度学习的模型 在第二部分,将建立几个时间序列模型来预测股市的应用案例,并了解一些时间序列建模技术.这些模型将相互比较, ...

  5. 时间序列预测 预测时间段_应用时间序列预测:美国住宅

    时间序列预测 预测时间段 1.简介 (1. Introduction) During these COVID19 months housing sector is rebounding rapidly ...

  6. 时间序列预测 时间因果建模_时间序列建模以预测投资基金的回报

    时间序列预测 时间因果建模 Time series analysis, discussed ARIMA, auto ARIMA, auto correlation (ACF), partial aut ...

  7. 时间序列预测任务PyTorch数据集类——TimeSeriesDataSet 类详解

    时间序列预测任务PyTorch数据集类--TimeSeriesDataSet 类详解 当进行时间序列预测或时间序列分析时,通常需要对数据进行预处理和转换以提高模型的效果和准确性.TimeSeriesD ...

  8. 时间序列预测步骤_建立时间序列预测模型的5个简单步骤

    时间序列预测步骤 I am a strong believer in "learning by doing" philosophy. 我坚信"做中学"的哲学. ...

  9. 序列每天从1开始_时间序列预测一

    什么是时间序列: 时间序列可以看作是普通的二维的无序的特征矩阵向时间空间的拓展,相对来说多了仅仅一个维度但也是非常重要的时间维度: 时间序列是按时间顺序进行的一系列观察,通常包括了连续性时间序列数据和 ...

  10. 独家 | 手把手教你用Python的Prophet库进行时间序列预测

    作者:Jason Brownlee 翻译:殷之涵 校对:吴振东 本文长度为4800字,建议阅读10+分钟 本文为大家介绍了如何在Python中使用由Facebook开发的Prophet库进行自动化的时 ...

最新文章

  1. Lab模式的妙用--人像处理
  2. Android成长之路-实现简单动画
  3. list删除某个元素_Redis对象——列表(List)
  4. service层的方法上使用@Transactional注解,程序发生异常没有回滚
  5. oracle常用命令收集
  6. android bugly 符号表,bugly cocos 接入和 符号表使用
  7. gridview中的图片错乱解决办法
  8. $.ajax提交,后台接受到的值总是乱码?明天再总结
  9. WPF 邮箱输入框提示 和 手机号码验证 .
  10. 学习如何在matlab用带通滤波器进行滤波
  11. STM32H7+LAN8720A之ETH与LWIP配置问题(End)
  12. 戴姆勒集团将拆分卡车业务;洲际酒店集团发布全新品牌标识;先正达集团中国创新研发中心落户南京 | 美通企业周刊...
  13. matlab画区间柱状图,科学网—Matlab画柱状图 - 高淑敏的博文
  14. 让MAC在TouchBar显示网速
  15. SW中如何将拉伸的物体转化为钣金件
  16. java 下载压缩文件后打开报 不可预料的压缩文件末端
  17. 针对Informer中时序数据ETT油温数据分析
  18. Carson带你学数据结构:手把手带你了解 ”图“ 所有知识!(含DFS、BFS)
  19. s一般怎么称呼自己的m_教师节到了,聊一聊该怎么称呼尊敬的老师
  20. idea Failed to clean project Failed to delete target

热门文章

  1. 中职学校计算机教学中,探析中职学校计算机教学中的一些体会和思考.doc
  2. Win7连接蓝牙耳机(千月蓝牙激活码分享)无需破解软件
  3. 用友2003年度NC_SCM项目经理/高级顾问认证考试试题及答案
  4. 【转载】财务主管的ERP实施之路
  5. Protel 99se汉化步骤
  6. Vista v12.0 Win32-ISO 1DVD(地震数据处理)
  7. 永中集成Office:既是创新产品,也是战略储备
  8. python制作自动交易软件-Python语言之一位程序员写了一个自动化交易程序,躺着玩,两年就挣了两百万!...
  9. 查询CI框架的版本号
  10. QtableWidget的表格加载图片设置