GTSPredictor module

class GTSPredictor.EarlyStoppingByUnderVal(monitor='val_loss', value=1e-05, verbose=0)

Bases: tensorflow.python.keras.callbacks.Callback

Class to stop model’s training earlier if the value we monitor(monitor) goes under a threshold (value) replace usual callbacks functions from keras

on_epoch_end(epoch, logs={})

Called at the end of an epoch.

Subclasses should override for any actions to run. This function should only be called during TRAIN mode.

Arguments:

epoch: integer, index of epoch. logs: dict, metric results for this training epoch, and for the

validation epoch if validation is performed. Validation result keys are prefixed with val_.

class GTSPredictor.GTSPredictor(verbose=0)

Bases: object

create_features(df, label=None)
fit(df, look_back, freq_period, verbose=0, nb_features=1, nb_epochs=200, nb_batch=100, nb_layers=50, attention=True, loss='mape', metric='mse', optimizer='Adamax', directory='.')

This function take the data , make the preprocessing steps and train the models whit multi threading

Parameters

dfdataframe

with a time columns (string) and y the columns with the values to forecast.

look_backint

size of inputs .

freq_periodint

size in point of the seasonal pattern (ex 24 if daily seasonality for a signal sampled at 1h frequency).

verbose0 or 1

If 0 no information printed during the creation and training of models.

nb_features :int, optional

output size. The default is 1.

nb_epochsint, optional

The default is 50.

nb_batchint, optional

The default is 100.

nb_layersint, optional

The default is 50.

attentionBool, optional

Either use or not the attention layer. The default is True.

lossstr, optional

loss for evaluation error between input and output . The default is “mape”.

metricstr, optional

metrics for evaluation the training predictions. The default is “mse”.

optimizerstr, optional

Keras optimizers. The default is “Adamax”.

directorystr, optional

directory path which need to end by “/”. The default is r”.”

Returns

None.

fit_predict_XGBoost(df, freq, date_format, steps=1, early_stopping_rounds=300, test_size=0.01, nb_estimators=1000)

This function uses XGBoost mode in order to forecast values.

Parameters

dfdataframe

Values that are going to be used for the training.

freqstr (python datetime format)

frequency of the data, ex “m” for months, “d” for days …

date_formatstr

format of time columns (ex if date columns is 1996-05-04, date_format=”%Y-%m-%d”

stepsint

number of points to forecast

Returns

predictionarray

array of predicted values

fit_predict_without_dec(df, look_back, freq_period, len_prediction, verbose, nb_features=1, nb_epochs=50, nb_batch=100, nb_layers=50, attention=True, loss='mape', metric='mse', optimizer='Adamax')
fit_whitout(df, look_back, freq_period, verbose=0, nb_features=1, nb_epochs=200, nb_batch=100, nb_layers=50, attention=True, loss='mape', metric='mse', optimizer='Adamax', directory='.')

This function take the data , make the preprocessing steps and train the models whitout multi threading

Parameters

dfdataframe

with a time columns (string) and y the columns with the values to forecast.

look_backint

size of inputs .

freq_periodint

size in point of the seasonal pattern (ex 24 if daily seasonality for a signal sampled at 1h frequency).

verbose0 or 1

If 0 no information printed during the creation and training of models.

nb_features :int, optional

output size. The default is 1.

nb_epochsint, optional

The default is 50.

nb_batchint, optional

The default is 100.

nb_layersint, optional

The default is 50.

attentionBool, optional

Either use or not the attention layer. The default is True.

lossstr, optional

loss for evaluation error between input and output . The default is “mape”.

metricstr, optional

metrics for evaluation the training predictions. The default is “mse”.

optimizerstr, optional

Keras optimizers. The default is “Adamax”.

directorystr, optional

directory path which need to end by “/”. The default is r”.”.

Returns

None

fit_with_UI(df, verbose=0, nb_features=1, nb_epochs=300, nb_batch=200, nb_layers=50, attention=True, loss='mape', metric='mse', optimizer='Adamax', directory='.')

This function open a matplotlib window for you to select the pattern, take the data , make the preprocessing steps and train the models

Parameters

dfdataframe

with a time columns (string) and y the columns with the values to forecast.

verbose0 or 1

If 0 no information printed during the creation and training of models.

nb_features :int, optional

output size. The default is 1.

nb_epochsint, optional

The default is 50.

nb_batchint, optional

The default is 100.

nb_layersint, optional

The default is 50.

attentionBool, optional

Either use or not the attention layer. The default is True.

lossstr, optional

loss for evaluation error between input and output . The default is “mape”.

metricstr, optional

metrics for evaluation the training predictions. The default is “mse”.

optimizerstr, optional

Keras optimizers. The default is “Adamax”.

directorystr, optional

directory path which need to end by “/”. The default is r”.”

Returns

None.

frame_prediction(prediction)

This function compute the 95% confident interval by calculating the standard deviation and the mean of the residual(Gaussian like distribution) and return yestimated +- 1.96*CI +mean (1.96 to have 95%)

Parameters

predictionarray

array contening the perdicted vector (1,N) size.

Returns

lowerarray

array contening the lowers boundry values (1,N) size.

upperarray

array contening the upper boundry values (1,N) size.

load_models(directory='.')
make_models(trend, df)
make_models_with(trend, df)

Create an LSTM model depending on the parameters selected by the user

Parameters

nb_layersint

nb of layers of the lstm model.

lossstr

loss of the model.

metricstr

metric to evaluate the model.

nb_featuresint

size of the ouput (one for regression).

optimizerstr

gradient descend’s optimizer.

trendbool

Distinguish trend signal from others (more complicated to modelise).

Returns

modelSequential object

model

make_prediction(len_prediction)
This function make the prediction :

reshape data to fit with the model make one prediction crush outdated data with prediction repeat len_prediction times

Parameters

len_predictionint

number of value we want to predict.

Returns

predictionarray

values predicted.

pred_trendarray

values trend predicted (recherches prupose).

pred_seasonalarray

values seasonal predicted (recherches prupose).

pred_residualarray

values residual predicted (recherches prupose).

model_save(model, name)
plot_prediction(df, prediction, lower=0, upper=0)

This function plots the predicted values at the end of the real values and plots the 95% CI.

Parameters

dfdataframe

Values that are going to be used for the training.

predictionarray

predicted values

lowerarray

lower bounder values

upperarray

upper bounder values.

predict(steps=1, frame=True)

Predict the futur values

Parameters

stepsint

number of points you want to forecast

frameBool

True if you want the lower and upper bounders.default is True.

Returns

predictionarray

Array with the predicted values

lowerarray

Array containing the lower values. Only if frame == True.

frameBool

Array containing the lower values. Only if frame == True.

prediction_eval(prediction, real_data)

This function compute and print four differents metrics (mse ,mae ,r2 and median) to evaluate accuracy of the model prediction and real_data need to have the same size

Parameters

predictionarray

predicted values.

real_dataarray

real data.

Returns

None.

prepare_data(df, look_back, freq_period, first=0)
Parameters

dfDataFrame

datafrmae contening historical data .

look_backint

length entry of the model .

Decompose the signal in three sub signals, trend,seasonal and residual in order to work separetly on each signal :returns: trend_x : array

values of the trend of the signal, matrix of dimention X array of dimension (1,length entry of model) X= length(dataframe)/look_back.

trend_yarray

vaklues to be predicted during the training

seasonal_xarray

same as trend_x but with the seasonal part of the signal.

seasonal_yarray

same as trend_y but with the seasonal part of the signal.

residual_xarray

same as trend_x but with the residual part of the signal.

residual_yarray

same as trend_y but with the residual part of the signal.

retrain(df, nb_features=1, nb_epochs=10, nb_batch=10)

This function retrain a GTSPredictor object that contained already trained models in order to do incremental learning.

Parameters

dfdataframe

Values that are going to be used for the training.

Returns

selfGTSPredictor object

trained object

reuse(df, directory='.', verbose=0)

This function recreate a new GTSPredictor object with already trained models in order to be then reused.

Parameters

dfdataframe

Values that are going to be used for the training.

directorydataframe

Directory containing the models to be load and the informations

verboseint

0 for no informations, 1 otherweise.

Returns

selfGTSPredictor object

trained object

train_model(model, x_train, y_train, name)

Train the model and save it in the right file

Parameters

modelSequential object

model.

x_trainarray

training data inputs.

y_trainarray

training data ouputs.

nb_epochsint.

nb of training repetitions.

nb_batchint

size of batch of element which gonna enter in the model before doing a back propagation.

trendbool

Distinguish trend signal from others (more complicated to modelise).

Returns

modelSequential object

model.

class GTSPredictor.IdentityTransformer

Bases: sklearn.base.BaseEstimator, sklearn.base.TransformerMixin

fit(input_array, y=None)
inverse_transform(input_array, y=None)
transform(input_array, y=None)
class GTSPredictor.Thread_train_model(model, q, x_train, y_train, nb_epochs, nb_batch, name_ts, name='', verbose=0)

Bases: threading.Thread

run()

Method representing the thread’s activity.

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

stop()
train_model(model, x_train, y_train, nb_epochs, nb_batch, name)

Train the model and save it in the right file

Parameters

modelSequential object

model.

x_trainarray

training data inputs.

y_trainarray

training data ouputs.

nb_epochsint.

nb of training repetitions.

nb_batchint

size of batch of element which gonna enter in the model before doing a back propagation.

trendbool

Distinguish trend signal from others (more complicated to modelise).

Returns

modelSequential object

model.

GTSPredictor.attention_block(hidden_states)
GTSPredictor.decoupe_dataframe(df, look_back)
GTSPredictor.progressbar(it, prefix='', size=60, file=<colorama.ansitowin32.StreamWrapper object>)