2021年12月10日記事を修正しました
はじめに
GluonTS 0.8.1 が公開されているのでWindows 10で使ってみました。TSは「Time Series」の略です。GluonTSは時系列データを扱うツールです。Pythonのバージョンは3.8.10です。GPU非搭載のノートPCで動作確認しています。GPUがなくても十分動作可能です。環境構築
「mxnet」「gluonts」をpipでインストールします。pip install mxnet -f https://dist.mxnet.io/python/cpu pip install gluonts
このようになりました。
certifi==2021.10.8 charset-normalizer==2.0.7 colorama==0.4.4 convertdate==2.3.2 cycler==0.10.0 gluonts==0.8.1 graphviz==0.17 hijri-converter==2.2.2 holidays==0.11.3.1 idna==3.3 kiwisolver==1.3.2 korean-lunar-calendar==0.2.1 matplotlib==3.4.3 mxnet==1.8.0 numpy==1.21.2 pandas==1.3.3 Pillow==8.3.2 pydantic==1.8.2 PyMeeus==0.5.11 pyparsing==2.4.7 python-dateutil==2.8.2 pytz==2021.3 requests==2.26.0 six==1.16.0 toolz==0.11.1 tqdm==4.62.3 typing-extensions==3.10.0.2 urllib3==1.26.7
Pythonスクリプト
import numpy as np import pandas as pd from matplotlib import pyplot as plt from mxnet.gluon.utils import download import zipfile import mxnet as mx zip_fname = download('https://archive.ics.uci.edu/ml/machine-learning-databases/00275/Bike-Sharing-Dataset.zip') with zipfile.ZipFile(zip_fname) as existing_zip: existing_zip.extractall('.') df = pd.read_csv('day.csv',index_col=1) feat1 = np.array(df.hum).reshape((1,-1)) feat2 = np.array(df.temp).reshape((1,-1)) feat3 = np.array(df.windspeed).reshape((1,-1)) feat4 = np.array(pd.get_dummies(df.weekday)).transpose((1,0)) feat5 = np.array(df.workingday).reshape((1,-1)) feat6 = np.array(pd.get_dummies(df.weathersit)).transpose((1,0)) feat7 = np.array(pd.get_dummies(df.season)).transpose((1,0)) features_real = np.concatenate([feat1, feat2, feat3, feat4, feat5, feat6, feat7], axis=0) from gluonts.dataset.common import ListDataset training_data = ListDataset( [{"start": df.index[0], "target": df.cnt[:-14], "feat_dynamic_real": features_real[:,:-14], }], freq = "1D") test_data = ListDataset( [{"start": df.index[0], "target": df.cnt, 'feat_dynamic_real': features_real, }], freq = "1D") from gluonts.model.deepar import DeepAREstimator from gluonts.mx.trainer import Trainer estimator = DeepAREstimator(freq="1D", prediction_length=14, context_length=28, use_feat_dynamic_real = True, trainer=Trainer(epochs=50, ctx=mx.cpu())) predictor = estimator.train(training_data=training_data) from gluonts.evaluation.backtest import make_evaluation_predictions from gluonts.dataset.util import to_pandas forecast_it, ts_it = make_evaluation_predictions( dataset=test_data, # test dataset predictor=predictor, # predictor num_samples=100, # number of sample paths we want for evaluation ) plot_length = 50 prediction_intervals = (50.0, 90.0) legend = ["observations", "median prediction"] + [f"{k}% prediction interval" for k in prediction_intervals][::-1] fig = plt.figure() for x, y in zip(test_data, forecast_it): to_pandas(x)[-plot_length:].plot() y.samples = y.samples.clip(min=0) y.plot(color='g', prediction_intervals=prediction_intervals) plt.grid(which='both') plt.legend(legend, loc='upper left') fig.savefig('result.png')
結果
問題なく実行できましたが2つの警告が出ました。警告
UserWarning: Using `json`-module for json-handling. Consider installing one of `orjson`, `ujson` to speed up serialization and deserialization.
FutureWarning: The 'freq' argument in Timestamp is deprecated and will be removed in a future version. FutureWarning: Timestamp.freq is deprecated and will be removed in a future version.