FeatureFlattener

class kooplearn.preprocessing.FeatureFlattener[source]

Bases: BaseEstimator, TransformerMixin

A scikit-learn compatible transformer that flattens multi-dimensional trajectories into a 2D array, and restores them to their original shape when inverted.

This transformer is useful when working with models that expect 2D input (e.g., (n_samples, n_features)), but the data naturally has higher-order structure, e.g., images or spatio-temporal fields.

Examples

>>> import numpy as np
>>> from sklearn.pipeline import make_pipeline
>>> from sklearn.preprocessing import StandardScaler
>>> from kooplearn.preprocessing import FeatureFlattener
>>>
>>> X = np.random.rand(10, 4, 5)  # e.g., 10 snapshots of a 4×5 field
>>> flattener = FeatureFlattener()
>>> X_flat = flattener.fit_transform(X)
>>> X_flat.shape
(10, 20)
>>> X_reconstructed = flattener.inverse_transform(X_flat)
>>> np.allclose(X, X_reconstructed)
True

Methods

fit(X, y=None)[source]

Store the original feature shape for later reconstruction.

Parameters:
  • X (ndarray of shape (n_samples, )) – Input data with arbitrary feature dimensions.

  • y (None) – Ignored. Present for API compatibility with scikit-learn pipelines.

Returns:

self – Fitted transformer instance.

Return type:

object

inverse_transform(X, y=None)[source]

Restore flattened features to their original shape.

Parameters:
  • X (ndarray of shape (n_samples, n_features)) – Flattened data to reconstruct.

  • y (None) – Ignored. Present for API compatibility with scikit-learn pipelines.

Returns:

Data reshaped to the original feature dimensions.

Return type:

ndarray of shape (n_samples, )

transform(X, y=None)[source]

Flatten input features into a 2D array.

Parameters:
  • X (ndarray of shape (n_samples, )) – Input data to flatten.

  • y (None) – Ignored. Present for API compatibility with scikit-learn pipelines.

Returns:

Flattened input data.

Return type:

ndarray of shape (n_samples, n_features)