tsfeatures

September 1, 2021 ยท View on GitHub

Build PyPI version fury.io Downloads Python 3.6+ License: MIT

tsfeatures

Calculates various features from time series data. Python implementation of the R package tsfeatures.

Installation

You can install the released version of tsfeatures from the Python package index with:

pip install tsfeatures

Usage

The tsfeatures main function calculates by default the features used by Montero-Manso, Talagala, Hyndman and Athanasopoulos in their implementation of the FFORMA model.

from tsfeatures import tsfeatures

This function receives a panel pandas df with columns unique_id, ds, y and optionally the frequency of the data.

<img src=https://raw.githubusercontent.com/FedericoGarza/tsfeatures/master/.github/images/y_train.png width="152">

tsfeatures(panel, freq=7)

By default (freq=None) the function will try to infer the frequency of each time series (using infer_freq from pandas on the ds column) and assign a seasonal period according to the built-in dictionary FREQS:

FREQS = {'H': 24, 'D': 1,
         'M': 12, 'Q': 4,
         'W':1, 'Y': 1}

You can use your own dictionary using the dict_freqs argument:

tsfeatures(panel, dict_freqs={'D': 7, 'W': 52})

List of available features

Features
acf_featuresheterogeneityseries_length
arch_statholt_parameterssparsity
count_entropyhurststability
crossing_pointshw_parametersstl_features
entropyintervalsunitroot_kpss
flat_spotslumpinessunitroot_pp
frequencynonlinearity
guerreropacf_features

See the docs for a description of the features. To use a particular feature included in the package you need to import it:

from tsfeatures import acf_features

tsfeatures(panel, freq=7, features=[acf_features])

You can also define your own function and use it together with the included features:

def number_zeros(x, freq):

    number = (x == 0).sum()
    return {'number_zeros': number}

tsfeatures(panel, freq=7, features=[acf_features, number_zeros])

tsfeatures can handle functions that receives a numpy array x and a frequency freq (this parameter is needed even if you don't use it) and returns a dictionary with the feature name as a key and its value.

R implementation

You can use this package to call tsfeatures from R inside python (you need to have installed R, the packages forecast and tsfeatures; also the python package rpy2):

from tsfeatures.tsfeatures_r import tsfeatures_r

tsfeatures_r(panel, freq=7, features=["acf_features"])

Observe that this function receives a list of strings instead of a list of functions.

Comparison with the R implementation (sum of absolute differences)

Non-seasonal data (100 Daily M4 time series)

featuredifffeaturedifffeaturedifffeaturediff
e_acf100e_acf10diff2_acf10alpha3.2
seasonal_period0spike0diff1_acf100arch_acf3.3
nperiods0curvature0x_acf10beta4.04
linearity0crossing_points0nonlinearity0garch_r24.74
hw_gamma0lumpiness0diff2x_pacf50hurst5.45
hw_beta0diff1x_pacf50unitroot_kpss0garch_acf5.53
hw_alpha0diff1_acf100x_pacf50entropy11.65
trend0arch_lm0x_acf100
flat_spots0diff1_acf10unitroot_pp0
series_length0stability0arch_r21.37

To replicate this results use:

python -m tsfeatures.compare_with_r --results_directory /some/path
                                    --dataset_name Daily --num_obs 100

Sesonal data (100 Hourly M4 time series)

featuredifffeaturedifffeaturedifffeaturediff
series_length0seas_acf10trend2.28hurst26.02
flat_spots0x_acf10arch_r22.29hw_beta32.39
nperiods0unitroot_kpss0alpha2.52trough35
crossing_points0nonlinearity0beta3.67peak69
seasonal_period0diff1_acf100linearity3.97
lumpiness0x_acf100curvature4.8
stability0seas_pacf0e_acf107.05
arch_lm0unitroot_pp0garch_r27.32
diff2_acf10spike0hw_gamma7.32
diff2_acf100seasonal_strength0.79hw_alpha7.47
diff1_acf10e_acf11.67garch_acf7.53
diff2x_pacf50arch_acf2.18entropy9.45

To replicate this results use:

python -m tsfeatures.compare_with_r --results_directory /some/path \
                                    --dataset_name Hourly --num_obs 100

Authors