Note
Click here to download the full example code
Transformer pipeline example¶
In this example, we will create a pipeline of transformers.
from __future__ import absolute_import, division, print_function, unicode_literals
import matplotlib.pyplot as plt
from future import standard_library
from numpy import allclose, linspace, matmul, sin
from gemseo.api import configure_logger
from gemseo.mlearning.transform.pipeline import Pipeline
from gemseo.mlearning.transform.scaler.scaler import Scaler
configure_logger()
standard_library.install_aliases()
Create dataset¶
x = linspace(0, 1, 100)
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)
# 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)
print(only_shift_jac)
print(only_scale_jac)
print(allclose(jac, matmul(only_scale_jac, only_shift_jac)))
Out:
[[0.5 0. 0. ... 0. 0. 0. ]
[0. 0.5 0. ... 0. 0. 0. ]
[0. 0. 0.5 ... 0. 0. 0. ]
...
[0. 0. 0. ... 0.5 0. 0. ]
[0. 0. 0. ... 0. 0.5 0. ]
[0. 0. 0. ... 0. 0. 0.5]]
[[1. 0. 0. ... 0. 0. 0.]
[0. 1. 0. ... 0. 0. 0.]
[0. 0. 1. ... 0. 0. 0.]
...
[0. 0. 0. ... 1. 0. 0.]
[0. 0. 0. ... 0. 1. 0.]
[0. 0. 0. ... 0. 0. 1.]]
[[0.5 0. 0. ... 0. 0. 0. ]
[0. 0.5 0. ... 0. 0. 0. ]
[0. 0. 0.5 ... 0. 0. 0. ]
...
[0. 0. 0. ... 0.5 0. 0. ]
[0. 0. 0. ... 0. 0.5 0. ]
[0. 0. 0. ... 0. 0. 0.5]]
True
Total running time of the script: ( 0 minutes 0.097 seconds)