mlnext.data.detemporalize#
- mlnext.data.detemporalize(data: ndarray, *, stride: int = 0, last_point_only: bool = False, verbose: bool = False) ndarray [source]#
Transforms a 3 dimensional array (rows, timesteps, features) into a 2 dimensional array (new_rows, features). If
stride
>= timesteps or 0, then the operation is equivalent todata.reshape(-1, features)
and new_rows equals rows * timesteps. If 0 <stride
< timesteps, the stride induced elements will be removed and new_rows equals (rows - timesteps) * timesteps. Iflast_point_only=True
then only the last point in each window is kept and new_rows equals (rows, features).- Parameters:
data (np.ndarray) – Array to transform.
stride (np.ndarray) – Stride that was used to transform the array from 2d into 3d.
last_point_only (np.ndarray) – Whether to only take the last point of each window.
verbose (bool) – Whether to print old and new shape.
- Returns:
Returns an array of shape (rows * 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 3d data into 2d >>> data_3d = mlnext.temporalize(data, timesteps=2) >>> print(data_3d) [[[ 0 1 2] [10 11 12]] [[20 21 22] [30 31 32]] [[40 41 42] [50 51 52]]] >>> mlnext.detemporalize(data_3d, verbose=True) Old shape: (3, 2, 3). New shape: (6, 3). [[ 0 1 2] [10 11 12] [20 21 22] [30 31 32] [40 41 42] [50 51 52]]
>>> # Transform 3d data into 2d with stride=1 >>> data_3d = mlnext.temporalize(data, ... timesteps=3, stride=1, verbose=True) Old shape: (6, 3). New shape: (4, 3, 3). >>> print(data_3d) [[[ 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]]] >>> mlnext.detemporalize(data_3d, stride=1, verbose=True) Old shape: (4, 3, 3). New shape: (6, 3). [[ 0 1 2] [10 11 12] [20 21 22] [30 31 32] [40 41 42] [50 51 52]] >>> # Take only the last point from each window >>> mlnext.detemporalize(data_3d, last_point_only=True, verbose=True) Old shape: (4, 3, 3). New shape: (4, 3). [[20 21 22] [30 31 32] [40 41 42] [50 51 52]]