Skip to main content

Package to forecast time series with recurrent neural network

Project description

Time_Series_Prediction_RNN

code-size license

Requirements

ts_rnn requires the following to run:

Installation

From pip

You could install the latest version from PyPi:

pip install ts-rnn

From Github

You could install the latest version directly from Github:

pip install https://github.com/LevPerla/Time_Series_Prediction_RNN/archive/master.zip

From source

Download the source code by cloning the repository or by pressing 'Download ZIP' on this page.

Install by navigating to the proper directory and running:

python setup.py install

Example

Example Open In Colab

Documentation

The full documentation haven't ready yet. I hope, it will show later.

Getting started

To import TS_RNN model run

from ts_rnn.model import TS_RNN

First of all, we need to set architecture of RNN in config in the way like this:

rnn_arch = {"layers": [
                        ["LSTM", {"units": 64,
                                  "return_sequences": False,
                                  "kernel_initializer": "glorot_uniform",
                                  "activation": "linear"}],
                        ["Dropout", {"rate": 0.2}],
                        ["Dense", {"activation": "linear"}]
                    ]}

WARNING: Last RNN block need to gave return_sequences: False, another - True

To set the architecture of RNN you can use some of this blocks:

# LSTM block
["LSTM", {Keras layer params}],
["GRU", {Keras layer params}],
["SimpleRNN", {Keras layer params}],
["Bidirectional", {Keras layer params}],
["Dropout", {Keras layer params}],
["Dense", {Keras layer params}]

TS_RNN class has 7 attributes:

  1. n_lags - length of the input vector;
  2. horizon - length of prediction horizon;
  3. rnn_arch - description of the model's parameters in Python dictionary format;
  4. strategy - prediction strategy: "Direct", "Recursive", "MiMo", "DirRec", "DirMo"
  5. tuner - tupe of Keras.tuner: "RandomSearch", "BayesianOptimization", "Hyperband"
  6. tuner_hp - keras_tuner.HyperParameters class
  7. n_step_out - length of the output vector (Need to define only for DirMo strategy);
  8. loss - Keras loss to train model;
  9. optimizer - Keras optimizer to train model.
  10. n_features - number of time series in the input (only for factors forecasting);
  11. save_dir - dir to save logs

You can set model this way:

model = TS_RNN(rnn_arch=rnn_arch,  # dict with model architecture
               n_lags=12,  # length of the input vector
               horizon=TEST_LEN,  # length of prediction horizon
               strategy="MiMo",  # Prediction strategy from "Direct", "Recursive", "MiMo", "DirRec", "DirMo"
               loss="mae",  # Keras loss
               optimizer="adam",  # Keras optimizer
               n_features=X_train.shape[1]  # also you need to define this if use factors
               )

TS_RNN supports 5 methods:

  1. fit - train the neural network;
  2. predict - predict by the neural network by input;
  3. forecast - predict by the neural network by last train values;
  4. summary - print NNs architecture
  5. save - save model files to dict

FIT

my_callbacks = [callbacks.EarlyStopping(patience=30, monitor='val_loss')]

model.fit(factors_train=factors_val,  # pd.DataFrame with factors time series
          target_train=target_val,  # pd.DataFrame or pd.Series with target time series
          factors_val=factors_val,  # pd.DataFrame with factors time series
          target_val=target_val,  # pd.DataFrame or pd.Series with target time series
          epochs=100,  # num epoch to train
          batch_size=12,  # batch_size
          callbacks=my_callbacks,  # Keras callbacks
          save_dir="../your_folder",  # folder to image save 
          verbose=2)  # verbose

PREDICT

predicted = model.predict(factors=factors_to_pred,
                          target=target_to_pred,
                          prediction_len=len(y_test))

FORECAST

predicted = model.forecast(prediction_len=HORIZON)

SUMMARY

model.summary()

SAVE

model.save(model_name='tsrnn_model', save_dir='path')

Also you may load TS_RNN model from folder

from ts_rnn.model import load_ts_rnn

model = load_ts_rnn(os.path.join('path', 'tsrnn_model'))

Simple example of usage:

Info: For better performance use MinMaxScaler and Deseasonalizer before fitting

from sklearn.model_selection import train_test_split
from ts_rnn.model import TS_RNN
import pandas as pd

HORIZON = 12

data_url = "https://raw.githubusercontent.com/LevPerla/Time_Series_Prediction_RNN/master/data/series_g.csv"
target = pd.read_csv(data_url, sep=";").series_g
target_train, target_test = train_test_split(target, test_size=HORIZON, shuffle=False)

model = TS_RNN(n_lags=12, horizon=HORIZON)
model.fit(target_train=target_train,
          target_val=target_test,
          epochs=40,
          batch_size=12,
          verbose=1)

model.summary()
predicted = model.predict(target=target_train[-model.n_lags:], prediction_len=HORIZON)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ts_rnn-0.2.12.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ts_rnn-0.2.12-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file ts_rnn-0.2.12.tar.gz.

File metadata

  • Download URL: ts_rnn-0.2.12.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.5

File hashes

Hashes for ts_rnn-0.2.12.tar.gz
Algorithm Hash digest
SHA256 18067c43590afc6194faf4e4b0fa49ac654e52d433d9dc909a73f86e1d0099e8
MD5 54e770ed623bb7c331d811366afbaef8
BLAKE2b-256 358678ae1f58c4eb11c5c33f7782b36a5ea0c721be48e5bb8c6f35eff9e507e2

See more details on using hashes here.

File details

Details for the file ts_rnn-0.2.12-py3-none-any.whl.

File metadata

  • Download URL: ts_rnn-0.2.12-py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.5

File hashes

Hashes for ts_rnn-0.2.12-py3-none-any.whl
Algorithm Hash digest
SHA256 a5dd8c90600e4aa27ed9932d78e570aeaae4361cec8ac1448e8e46ad16ed6723
MD5 9f3abe18269eb879dfebe46311abfd7d
BLAKE2b-256 8fcd07c6a979fdec923ec772e40818b05e19e0ef0103e552aa54c7822f09e479

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page