Stationarizer¶
-
class
gtda.time_series.
Stationarizer
(operation='return')[source]¶ Methods for stationarizing time series data.
Time series may be stationarized to remove or reduce linear or exponential trends.
- Parameters
operation (
'return'
|'log-return'
, default:'return'
) –The type of stationarization operation to perform. It can have two values:
'return'
: This option transforms the time series \({X_t}_t\) into the time series of relative returns, i.e. the ratio \((X_t-X_{ t-1})/X_t\).'log-return'
: This option transforms the time series \({X_t}_t\) into the time series of relative log-returns, i.e. \(\log(X_t/X_{ t-1})\).
Examples
>>> import numpy as np >>> from gtda.time_series import Stationarizer >>> # Create a noisy signal >>> signal = np.asarray([np.sin(x /40) + 5 + np.random.random() >>> for x in range(0, 300)]).reshape(-1, 1) >>> # Initialize the stationarizer >>> stationarizer = Stationarizer(operation='return') >>> # Fit and transform the signal >>> signal_stationarized = stationarizer.fit_transform(signal) >>> print(signal_stationarized.shape) (299,)
-
fit
(X, y=None)[source]¶ Do nothing and return the estimator unchanged.
This method is here to implement the usual scikit-learn API and hence work in pipelines.
- Parameters
X (ndarray of shape (n_samples,) or (n_samples, ..)) – Input data.
y (None) – Ignored.
- Returns
self
- Return type
object
-
fit_transform
(X, y=None, **fit_params)¶ Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
- Parameters
X (ndarray of shape (n_samples, ..)) – Input data.
y (None) – There is no need for a target in a transformer, yet the pipeline API requires this parameter.
- Returns
Xt – Transformed input.
- Return type
numpy array of shape (n_samples, ..)
-
fit_transform_resample
(X, y, **fit_params)¶ Fit to data, then transform the input and resample the target. Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X ans a resampled version of y.
- Parameters
X (ndarray of shape (n_samples, ..)) – Input data.
y (ndarray of shape (n_samples,)) – Target data.
- Returns
Xt (ndarray of shape (n_samples, …)) – Transformed input.
yr (ndarray of shape (n_samples, …)) – Resampled target.
-
get_params
(deep=True)¶ Get parameters for this estimator.
- Parameters
deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns
params – Parameter names mapped to their values.
- Return type
mapping of string to any
-
resample
(y, X=None)[source]¶ Resample y.
- Parameters
y (ndarray of shape (n_samples,)) – Target.
X (None) – There is no need for input data, yet the pipeline API requires this parameter.
- Returns
yr – Resampled target.
n_samples_new = n_samples - 1
.- Return type
ndarray of shape (n_samples_new,)
-
set_params
(**params)¶ Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form
<component>__<parameter>
so that it’s possible to update each component of a nested object.- Parameters
**params (dict) – Estimator parameters.
- Returns
self – Estimator instance.
- Return type
object
-
transform
(X, y=None)[source]¶ Stationarize X by applying the procedure given by operation.
- Parameters
X (ndarray of shape (n_samples,) or (n_samples, ..)) – Input data.
y (None) – There is no need for a target, yet the pipeline API requires this parameter.
- Returns
Xt – Stationarized array.
n_samples_new = n_samples - 1
.- Return type
ndarray of shape (n_samples_new, ..)
-
transform_resample
(X, y)¶ Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
- Parameters
X (ndarray of shape (n_samples, ..)) – Input data.
y (ndarray of shape (n_samples,)) – Target data.
- Returns
Xt (ndarray of shape (n_samples, …)) – Transformed input.
yr (ndarray of shape (n_samples, …)) – Resampled target.