Note
Go to the end to download the full example code
Transformer pipeline example¶
In this example, we will create a pipeline of transformers.
from __future__ import annotations
import matplotlib.pyplot as plt
from gemseo import configure_logger
from gemseo.mlearning.transformers.pipeline import Pipeline
from gemseo.mlearning.transformers.scaler.scaler import Scaler
from numpy import allclose
from numpy import linspace
from numpy import matmul
from numpy import sin
configure_logger()
<RootLogger root (INFO)>
Create dataset¶
x = linspace(0, 1, 100)[:, None]
data = sin(10 * x) - 3 * x
Create transformer pipeline¶
We create a pipeline of two transformers; the first performing a shift, the second a scale (both scalers). This could also be achieved using one scaler, but we here present a pipeline doing these transformations separately for illustrative purposes.
shift = Scaler(offset=5)
scale = Scaler(coefficient=0.5)
pipeline = Pipeline(transformers=[shift, scale])
Transform data¶
In order to use the transformer, we have to fit it to the data.
pipeline.fit(data)
WARNING - 07:05:53: The Scaler.fit() function does nothing; the instance of Scaler uses the coefficient and offset passed at its initialization
WARNING - 07:05:53: The Scaler.fit() function does nothing; the instance of Scaler uses the coefficient and offset passed at its initialization
Transform data using the pipeline
transformed_data = pipeline.transform(data)
Transform data using individual components of the pipeline
only_shifted_data = shift.transform(data)
Plot data¶
plt.plot(x, data, label="Original data")
plt.plot(x, transformed_data, label="Shifted and scaled data")
plt.plot(x, only_shifted_data, label="Shifted but not scaled data")
plt.legend()
plt.show()

Compute jacobian¶
jac = pipeline.compute_jacobian(data)
only_shift_jac = shift.compute_jacobian(data)
only_scale_jac = scale.compute_jacobian(only_shifted_data)
print(jac.shape)
print(only_shift_jac.shape)
print(only_scale_jac.shape)
print(allclose(jac, matmul(only_scale_jac, only_shift_jac)))
(100, 1, 1)
(100, 1, 1)
(100, 1, 1)
True
Total running time of the script: ( 0 minutes 0.140 seconds)