Basic knowledge of MXNET and GLUon is required to read this manual. This article uses CPU training code. Zmkwjx.github. IO Github address: github.com/zmkwjx/Gluo… GluonTS official website: Gluon-ts.mxnet. IO
1. Environment and installation
1.1 Development environment: Ubuntu16.04 TS, PYTHon3.7 1.2 Quick installation
pip install matplotlib numpy pandas pathlib
pip install mxnet mxnet-mkl gluon gluonts
Copy the code
2. Training procedures
#Third-party imports
import matplotlib.pyplot as plt
import pandas as pd
from pathlib import Path
from gluonts.model import deepar
from gluonts.dataset import common
from gluonts.dataset.util import to_pandas
from gluonts.model.predictor import Predictor
Copy the code
2.1 Loading training data twitter_volume_amzn.csv
url = "./data/Twitter_volume_AMZN.csv"
df = pd.read_csv(url, header=0, index_col=0)
data = common.ListDataset([{"start": df.index[0]."target": df.value[:"The 2015-04-23 00:00:00"]}], freq="H")
Copy the code
- Pd. read_csv reads in the CSV file and converts it to a data box
- Common. ListDataset loads training data
2.2 interpretationListDataset
class gluonts.dataset.common.ListDataset(data_iter: Iterable[Dict[str, Any]], freq: str, one_dim_target: bool = True)
- Data_iter: The iterable produces all the items in the data set. Each item should be a dictionary that maps strings to values. Example: {” start “:” 2014-09-07 “, “target” : [0.1, 0.2]}
- Freq: Observation frequency in time series.
- One_dim_target: whether to accept only univariate target time series.
2.3 Training existing Models GluonTS comes with many pre-built models. All the user needs to do is configure some hyperparameters. Existing models focus on, but are not limited to, probabilistic predictions. Probabilistic prediction is a prediction in the form of probability distribution, rather than a simple single point estimation.
estimator = deepar.DeepAREstimator(freq="H", prediction_length=24)
predictor = estimator.train(training_data=data)
Copy the code
- Build a DeepAR network and train it
- Prediction_length: The length of time to predict
- Training_data: Indicates training data
2.4 Preview training results
for test_entry, forecast in zip(train_data, predictor.predict(train_data)):
to_pandas(test_entry)[-60:].plot(linewidth=2)
forecast.plot(color='g', prediction_intervals=[50.0.90.0])
plt.grid(which='both')
plt.show()
Copy the code
- Predicted results
2.5 Output training Results
prediction = next(predictor.predict(train_data))
print(prediction.mean)
prediction.plot(output_file='graph.png')
Copy the code
- OUT
2.5 Save the training model
predictor.serialize(Path("Fill in the absolute path to the Model folder here."))
Copy the code
2.6 Using the training model
predictor = Predictor.deserialize(Path("Fill in the absolute path to the Model folder here."))
Copy the code
- example
import pandas as pd
from pathlib import Path
from gluonts.dataset import common
from gluonts.dataset.util import to_pandas
from gluonts.model.predictor import Predictor
url = "./data/Twitter_volume_AMZN.csv"
df = pd.read_csv(url, header=0, index_col=0)
train_data = common.ListDataset([{"start": df.index[0]."target": df.value[:"The 2015-04-23 00:00:00"]}],freq="H")
predictor = Predictor.deserialize(Path("Fill in the absolute path to the Model folder here."))
prediction = next(predictor.predict(train_data))
print(prediction.mean)
prediction.plot(output_file='graph.png')
Copy the code