BagOfPatterns#

class pyts.transformation.BagOfPatterns(window_size: int | float = 0.5, word_size: int | float = 0.5, n_bins: int = 4, strategy: str = 'normal', numerosity_reduction: bool = True, window_step: int | float = 1, norm_mean: bool = True, norm_std: bool = True, sparse: bool = True, overlapping: bool = True, alphabet: ArrayLike | None = None)[source]#

Bag-of-patterns representation for time series.

This algorithm uses a sliding window to extract subsequences from the time series and transforms each subsequence into a word using the Piecewise Aggregate Approximation and the Symbolic Aggregate approXimation algorithms. Thus it transforms each time series into a bag of words. Then it derives the frequencies of each word for each time series.

Parameters:
window_sizeint or float (default = 0.5)

Length of the sliding window. If float, it represents a percentage of the size of each time series and must be between 0 and 1.

word_sizeint or float (default = 0.5)

Length of the words. If float, it represents a percentage of the length of the sliding window and must be between 0. and 1.

n_binsint (default = 4)

The number of bins to produce. It must be between 2 and min(window_size, 26).

strategy‘uniform’, ‘quantile’ or ‘normal’ (default = ‘normal’)

Strategy used to define the widths of the bins:

  • ‘uniform’: All bins in each sample have identical widths

  • ‘quantile’: All bins in each sample have the same number of points

  • ‘normal’: Bin edges are quantiles from a standard normal distribution

numerosity_reductionbool (default = True)

If True, delete sample-wise all but one occurence of back to back identical occurences of the same words.

window_stepint or float (default = 1)

Step of the sliding window. If float, it represents the percentage of the size of each time series and must be between 0 and 1. The step of sliding window will be computed as ceil(window_step * n_timestamps).

norm_meanbool (default = True)

If True, center each subseries before scaling.

norm_stdbool (default = True)

If True, scale each subseries to unit variance.

sparsebool (default = True)

Return a sparse matrix if True, else return an array.

overlappingbool (default = True)

If True, time points may belong to two bins when decreasing the size of the subsequence with the Piecewise Aggregate Approximation algorithm. If False, each time point belong to one single bin, but the size of the bins may vary.

alphabetNone or array-like, shape = (n_bins,)

Alphabet to use. If None, the first n_bins letters of the Latin alphabet are used.

Attributes:
vocabulary_dict

A mapping of feature indices to terms.

References

[1]

J. Lin, R. Khade and Y. Li, “Rotation-invariant similarity in time series using bag-of-patterns representation”. Journal of Intelligent Information Systems, 39 (2), 287-315 (2012).

Examples

>>> import numpy as np
>>> from pyts.transformation import BagOfPatterns
>>> X = np.arange(12).reshape(2, 6)
>>> bop = BagOfPatterns(window_size=4, word_size=4, sparse=False)
>>> bop.fit_transform(X)
array(...)
>>> bop.set_params(numerosity_reduction=False)
BagOfPatterns(...)
>>> bop.fit_transform(X)
array(...)

Methods

__init__([window_size, word_size, n_bins, ...])

fit(X[, y])

Learn the dictionary.

fit_transform(X[, y])

Derive word frequencies for each time series.

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

set_params(**params)

Set the parameters of this estimator.

transform(X)

Derive word frequencies for each time series.

__init__(window_size: int | float = 0.5, word_size: int | float = 0.5, n_bins: int = 4, strategy: str = 'normal', numerosity_reduction: bool = True, window_step: int | float = 1, norm_mean: bool = True, norm_std: bool = True, sparse: bool = True, overlapping: bool = True, alphabet: ArrayLike | None = None) → None[source]#
fit(X: ArrayLike, y: ArrayLike | None = None) → Self[source]#

Learn the dictionary.

Parameters:
Xarray-like, shape = (n_samples, n_timestamps)

Input data

y

Ignored

Returns:
selfobject
fit_transform(X: ArrayLike, y: ArrayLike | None = None) → NDArray[int64] | csr_matrix[source]#

Derive word frequencies for each time series.

Parameters:
Xarray-like, shape = (n_samples, n_timestamps)

Data to transform.

y

Ignored

Returns:
X_newarray, shape = (n_samples, n_words)

Word frequencies.

transform(X: ArrayLike) → NDArray[int64] | csr_matrix[source]#

Derive word frequencies for each time series.

Parameters:
Xarray-like, shape = (n_samples, n_timestamps)

Data to transform.

Returns:
X_newarray, shape = (n_samples, n_words)

Word frequencies.

Examples using pyts.transformation.BagOfPatterns#

Bag of Patterns

Bag of Patterns