mlnext.data.temporalize#

mlnext.data.temporalize(data: DataFrame | ndarray, *, timesteps: int, stride: int = 0, verbose: bool = False) ndarray[source]#

Transforms a 2 dimensional array (rows, features) into a 3 dimensional array of shape (new_rows, timesteps, features). The step size along axis 0 can be set with stride. If stride=0 or stride=timesteps, the operation is equivalent to data.reshape(-1, timesteps, features). Note: if rows % timesteps != 0 some rows might be discarded.

Parameters:
  • data (pd.DataFrame, np.ndarray) – Data to transform.

  • timesteps (int) – Number of timesteps.

  • stride (int) – Step size along the first axis (Default: 0).

  • verbose (bool) – Whether to print status information.

Returns:

Returns an array of shape rows x timesteps x features.

Return type:

np.ndarray

Example

>>> import numpy as np
>>> import mlnext
>>> # setup data
>>> i, j = np.ogrid[:6, :3]
>>> data = 10 * i + j
>>> print(data)
[[ 0  1  2]
 [10 11 12]
 [20 21 22]
 [30 31 32]
 [40 41 42]
 [50 51 52]]
>>> # Transform 2d data into 3d
>>> mlnext.temporalize(data=data, timesteps=2, verbose=True)
Old shape: (6, 2). New shape: (3, 2, 3).
[[[ 0  1  2]
  [10 11 12]]
  [[20 21 22]
   [30 31 32]]
  [[40 41 42]
   [50 51 52]]]
>>> # Transform 2d into 3d with stride=1
>>> mlnext.temporalize(data, timesteps=3, stride=1, verbose=True)
Old shape: (6, 3). New shape: (4, 3, 3).
[[[ 0  1  2]
  [10 11 12]
  [20 21 22]]
 [[10 11 12]
  [20 21 22]
  [30 31 32]]
 [[20 21 22]
  [30 31 32]
  [40 41 42]]
 [[30 31 32]
  [40 41 42]
  [50 51 52]]]