Table Of Contents
Table Of Contents

gluonts.transform module

class gluonts.transform.AddAgeFeature(target_field: str, output_field: str, pred_length: int, log_scale: bool = True, dtype: gluonts.core.component.DType = <class 'numpy.float32'>)[source]

Bases: gluonts.transform.MapTransformation

Adds an ‘age’ feature to the data_entry.

The age feature starts with a small value at the start of the time series and grows over time.

If is_train=True the age feature has the same length as the target field. If is_train=False the age feature has length len(target) + pred_length

Parameters:
  • target_field – Field with target values (array) of time series
  • output_field – Field name to use for the output.
  • pred_length – Prediction length
  • log_scale – If set to true the age feature grows logarithmically otherwise linearly over time.
map_transform(data: Dict[str, Any], is_train: bool) → Dict[str, Any][source]
class gluonts.transform.AddConstFeature(output_field: str, target_field: str, pred_length: int, const: float = 1.0, dtype: gluonts.core.component.DType = <class 'numpy.float32'>)[source]

Bases: gluonts.transform.MapTransformation

Expands a const value along the time axis as a dynamic feature, where the T-dimension is defined as the sum of the pred_length parameter and the length of a time series specified by the target_field.

If is_train=True the feature matrix has the same length as the target field. If is_train=False the feature matrix has length len(target) + pred_length

Parameters:
  • output_field – Field name for output.
  • target_field – Field containing the target array. The length of this array will be used.
  • pred_length – Prediction length (this is necessary since features have to be available in the future)
  • const – Constant value to use.
  • dtype – Numpy dtype to use for resulting array.
map_transform(data: Dict[str, Any], is_train: bool) → Dict[str, Any][source]
class gluonts.transform.AddObservedValuesIndicator(target_field: str, output_field: str, dummy_value: int = 0, convert_nans: bool = True, dtype: gluonts.core.component.DType = <class 'numpy.float32'>)[source]

Bases: gluonts.transform.SimpleTransformation

Replaces missing values in a numpy array (NaNs) with a dummy value and adds an “observed”-indicator that is 1 when values are observed and 0 when values are missing.

Parameters:
  • target_field – Field for which missing values will be replaced
  • output_field – Field name to use for the indicator
  • dummy_value – Value to use for replacing missing values.
  • convert_nans – If set to true (default) missing values will be replaced. Otherwise they will not be replaced. In any case the indicator is included in the result.
transform(data: Dict[str, Any]) → Dict[str, Any][source]
class gluonts.transform.AddTimeFeatures(start_field: str, target_field: str, output_field: str, time_features: List[gluonts.time_feature._base.TimeFeature], pred_length: int)[source]

Bases: gluonts.transform.MapTransformation

Adds a set of time features.

If is_train=True the feature matrix has the same length as the target field. If is_train=False the feature matrix has length len(target) + pred_length

Parameters:
  • start_field – Field with the start time stamp of the time series
  • target_field – Field with the array containing the time series values
  • output_field – Field name for result.
  • time_features – list of time features to use.
  • pred_length – Prediction length
map_transform(data: Dict[str, Any], is_train: bool) → Dict[str, Any][source]
class gluonts.transform.AdhocTransform(func: Callable[Dict[str, Any], Dict[str, Any]])[source]

Bases: gluonts.transform.SimpleTransformation

Applies a function as a transformation This is called ad-hoc, because it is not serializable. It is OK to use this for experiments and outside of a model pipeline that needs to be serialized.

transform(data: Dict[str, Any]) → Dict[str, Any][source]
class gluonts.transform.AsNumpyArray(field: str, expected_ndim: int, dtype: gluonts.core.component.DType = <class 'numpy.float32'>)[source]

Bases: gluonts.transform.SimpleTransformation

Converts the value of a field into a numpy array.

Parameters:
  • expected_ndim – Expected number of dimensions. Throws an exception if the number of dimensions does not match.
  • dtype – numpy dtype to use.
transform(data: Dict[str, Any]) → Dict[str, Any][source]
class gluonts.transform.BucketInstanceSampler(scale_histogram: gluonts.dataset.stat.ScaleHistogram)[source]

Bases: gluonts.transform.InstanceSampler

This sample can be used when working with a set of time series that have a skewed distributions. For instance, if the dataset contains many time series with small values and few with large values.

The probability of sampling from bucket i is the inverse of its number of elements.

Parameters:scale_histogram – The histogram of scale for the time series. Here scale is the mean abs value of the time series.
class gluonts.transform.CanonicalInstanceSplitter(target_field: str, is_pad_field: str, start_field: str, forecast_start_field: str, instance_sampler: gluonts.transform.InstanceSampler, instance_length: int, output_NTC: bool = True, time_series_fields: List[str] = [], allow_target_padding: bool = False, pad_value: float = 0.0, use_prediction_features: bool = False, prediction_length: Optional[int] = None)[source]

Bases: gluonts.transform.FlatMapTransformation

Selects instances, by slicing the target and other time series like arrays at random points in training mode or at the last time point in prediction mode. Assumption is that all time like arrays start at the same time point.

In training mode, the returned instances contain past_`target_field` as well as past_`time_series_fields`.

In prediction mode, one can set use_prediction_features to get future_`time_series_fields`.

If the target array is one-dimensional, the target_field in the resulting instance has shape (instance_length). In the multi-dimensional case, the instance has shape (dim, instance_length), where dim can also take a value of 1.

In the case of insufficient number of time series values, the transformation also adds a field ‘past_is_pad’ that indicates whether values where padded or not, and the value is padded with default_pad_value with a default value 0. This is done only if allow_target_padding is True, and the length of target is smaller than instance_length.

Parameters:
  • target_field – fields that contains time-series
  • is_pad_field – output field indicating whether padding happened
  • start_field – field containing the start date of the time series
  • forecast_start_field – field containing the forecast start date
  • instance_sampler – instance sampler that provides sampling indices given a time-series
  • instance_length – length of the target seen before making prediction
  • output_NTC – whether to have time series output in (time, dimension) or in (dimension, time) layout
  • time_series_fields – fields that contains time-series, they are split in the same interval as the target
  • allow_target_padding – flag to allow padding
  • pad_value – value to be used for padding
  • use_prediction_features – flag to indicate if prediction range features should be returned
  • prediction_length – length of the prediction range, must be set if use_prediction_features is True
flatmap_transform(data: Dict[str, Any], is_train: bool) → Iterator[Dict[str, Any]][source]
class gluonts.transform.Chain(trans: List[gluonts.transform.Transformation])[source]

Bases: gluonts.transform.Transformation

Chain multiple transformations together.

estimate(data_it: Iterator[Dict[str, Any]]) → Iterator[Dict[str, Any]][source]
class gluonts.transform.ConcatFeatures(output_field: str, input_fields: List[str], drop_inputs: bool = True)[source]

Bases: gluonts.transform.SimpleTransformation

Concatenate fields together using np.concatenate.

Fields with value None are ignored.

Parameters:
  • output_field – Field name to use for the output
  • input_fields – Fields to stack together
  • drop_inputs – If set to true the input fields will be dropped.
transform(data: Dict[str, Any]) → Dict[str, Any][source]
class gluonts.transform.ExpandDimArray(field: str, axis: Optional[int] = None)[source]

Bases: gluonts.transform.SimpleTransformation

Expand dims in the axis specified, if the axis is not present does nothing. (This essentially calls np.expand_dims)

Parameters:
  • field – Field in dictionary to use
  • axis – Axis to expand (see np.expand_dims for details)
transform(data: Dict[str, Any]) → Dict[str, Any][source]
class gluonts.transform.ExpectedNumInstanceSampler(num_instances: float)[source]

Bases: gluonts.transform.InstanceSampler

Keeps track of the average time series length and adjusts the probability per time point such that on average num_instances training examples are generated per time series.

Parameters:num_instances – number of training examples generated per time series on average
class gluonts.transform.FilterTransformation(condition: Callable[Dict[str, Any], bool])[source]

Bases: gluonts.transform.FlatMapTransformation

flatmap_transform(data: Dict[str, Any], is_train: bool) → Iterator[Dict[str, Any]][source]
class gluonts.transform.FlatMapTransformation[source]

Bases: gluonts.transform.Transformation

Transformations that yield zero or more results per input, but do not combine elements from the input stream.

flatmap_transform(data: Dict[str, Any], is_train: bool) → Iterator[Dict[str, Any]][source]
class gluonts.transform.Identity[source]

Bases: gluonts.transform.Transformation

class gluonts.transform.InstanceSampler[source]

Bases: object

An InstanceSampler is called with the time series and the valid index bounds a, b and should return a set of indices a <= i <= b at which training instances will be generated.

The object should be called with:

Parameters:
  • ts – target that should be sampled with shape (dim, seq_len)
  • a – first index of the target that can be sampled
  • b – last index of the target that can be sampled
Returns:

Selected points to sample

Return type:

np.ndarray

class gluonts.transform.InstanceSplitter(target_field: str, is_pad_field: str, start_field: str, forecast_start_field: str, train_sampler: gluonts.transform.InstanceSampler, past_length: int, future_length: int, output_NTC: bool = True, time_series_fields: Optional[List[str]] = None, pick_incomplete: bool = True)[source]

Bases: gluonts.transform.FlatMapTransformation

Selects training instances, by slicing the target and other time series like arrays at random points in training mode or at the last time point in prediction mode. Assumption is that all time like arrays start at the same time point.

The target and each time_series_field is removed and instead two corresponding fields with prefix past_ and future_ are included. E.g.

If the target array is one-dimensional, the resulting instance has shape (len_target). In the multi-dimensional case, the instance has shape (dim, len_target).

target -> past_target and future_target

The transformation also adds a field ‘past_is_pad’ that indicates whether values where padded or not.

Convention: time axis is always the last axis.

Parameters:
  • target_field – field containing the target
  • is_pad_field – output field indicating whether padding happened
  • start_field – field containing the start date of the time series
  • forecast_start_field – output field that will contain the time point where the forecast starts
  • train_sampler – instance sampler that provides sampling indices given a time-series
  • past_length – length of the target seen before making prediction
  • future_length – length of the target that must be predicted
  • output_NTC – whether to have time series output in (time, dimension) or in (dimension, time) layout
  • time_series_fields – fields that contains time-series, they are split in the same interval as the target
  • pick_incomplete – whether training examples can be sampled with only a part of past_length time-units present for the time series. This is useful to train models for cold-start. In such case, is_pad_out contains an indicator whether data is padded or not.
flatmap_transform(data: Dict[str, Any], is_train: bool) → Iterator[Dict[str, Any]][source]
class gluonts.transform.ListFeatures(output_field: str, input_fields: List[str], drop_inputs: bool = True)[source]

Bases: gluonts.transform.SimpleTransformation

Creates a new field which contains a list of features.

Parameters:
  • output_field – Field name for output
  • input_fields – Fields to combine into list
  • drop_inputs – If true the input fields will be removed from the result.
transform(data: Dict[str, Any]) → Dict[str, Any][source]
class gluonts.transform.MapTransformation[source]

Bases: gluonts.transform.Transformation

Base class for Transformations that returns exactly one result per input in the stream.

map_transform(data: Dict[str, Any], is_train: bool) → Dict[str, Any][source]
class gluonts.transform.RemoveFields(field_names: List[str])[source]

Bases: gluonts.transform.SimpleTransformation

transform(data: Dict[str, Any]) → Dict[str, Any][source]
class gluonts.transform.RenameFields(mapping: Dict[str, str])[source]

Bases: gluonts.transform.SimpleTransformation

Rename fields using a mapping

Parameters:mapping – Name mapping input_name -> output_name
transform(data: Dict[str, Any])[source]
class gluonts.transform.SelectFields(input_fields: List[str])[source]

Bases: gluonts.transform.MapTransformation

Only keep the listed fields

Parameters:input_fields – List of fields to keep.
map_transform(data: Dict[str, Any], is_train: bool) → Dict[str, Any][source]
class gluonts.transform.SetField(output_field: str, value: Any)[source]

Bases: gluonts.transform.SimpleTransformation

Sets a field in the dictionary with the given value.

Parameters:
  • output_field – Name of the field that will be set
  • value – Value to be set
transform(data: Dict[str, Any]) → Dict[str, Any][source]
class gluonts.transform.SetFieldIfNotPresent(field: str, value: Any)[source]

Bases: gluonts.transform.SimpleTransformation

Sets a field in the dictionary with the given value, in case it does not exist already

Parameters:
  • output_field – Name of the field that will be set
  • value – Value to be set
transform(data: Dict[str, Any]) → Dict[str, Any][source]
class gluonts.transform.SimpleTransformation[source]

Bases: gluonts.transform.MapTransformation

Element wise transformations that are the same in train and test mode

map_transform(data: Dict[str, Any], is_train: bool) → Dict[str, Any][source]
transform(data: Dict[str, Any]) → Dict[str, Any][source]
class gluonts.transform.SwapAxes(input_fields: List[str], axes: Tuple[int, int])[source]

Bases: gluonts.transform.SimpleTransformation

Apply np.swapaxes to fields.

Parameters:
  • input_fields – Field to apply to
  • axes – Axes to use
swap(v)[source]
transform(data: Dict[str, Any]) → Dict[str, Any][source]
class gluonts.transform.TestSplitSampler[source]

Bases: gluonts.transform.InstanceSampler

Sampler used for prediction. Always selects the last time point for splitting i.e. the forecast point for the time series.

class gluonts.transform.Transformation[source]

Bases: object

Base class for all Transformations.

A Transformation processes works on a stream (iterator) of dictionaries.

estimate(data_it: Iterator[Dict[str, Any]]) → Iterator[Dict[str, Any]][source]
class gluonts.transform.TransformedDataset(base_dataset: gluonts.dataset.common.Dataset, transformations: List[gluonts.transform.Transformation])[source]

Bases: gluonts.dataset.common.Dataset

A dataset that corresponds to applying a list of transformations to each element in the base_dataset. This only supports SimpleTransformations, which do the same thing at prediction and training time.

Parameters:
  • base_dataset – Dataset to transform
  • transformations – List of transformations to apply
class gluonts.transform.UniformSplitSampler(p: float = 0.05)[source]

Bases: gluonts.transform.InstanceSampler

Samples each point with the same fixed probability.

Parameters:p – Probability of selecting a time point
class gluonts.transform.VstackFeatures(output_field: str, input_fields: List[str], drop_inputs: bool = True)[source]

Bases: gluonts.transform.SimpleTransformation

Stack fields together using np.vstack.

Fields with value None are ignored.

Parameters:
  • output_field – Field name to use for the output
  • input_fields – Fields to stack together
  • drop_inputs – If set to true the input fields will be dropped.
transform(data: Dict[str, Any]) → Dict[str, Any][source]
gluonts.transform.serialize_data_entry(data_entry: Dict[str, Any]) → Dict[source]

Encode the numpy values in the a DataEntry dictionary into lists so the dictionary can be json serialized.

gluonts.transform.shift_timestamp(ts: pandas._libs.tslibs.timestamps.Timestamp, offset: int) → pandas._libs.tslibs.timestamps.Timestamp[source]

Computes a shifted timestamp.

Basic wrapping around pandas ts + offset with caching and exception handling.

gluonts.transform.target_transformation_length(target: numpy.core.multiarray.array, pred_length: int, is_train: bool) → int[source]