.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/mlearning/transformer/plot_pipeline.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_mlearning_transformer_plot_pipeline.py: Pipeline ======== .. GENERATED FROM PYTHON SOURCE LINES 25-35 .. code-block:: Python from __future__ import annotations from numpy import allclose from numpy import linspace from numpy import newaxis from gemseo.mlearning.transformers.pipeline import Pipeline from gemseo.mlearning.transformers.scaler.scaler import Scaler .. GENERATED FROM PYTHON SOURCE LINES 36-38 To illustrate the pipeline, we consider very simple data: .. GENERATED FROM PYTHON SOURCE LINES 38-40 .. code-block:: Python data = linspace(0, 1, 100)[:, newaxis] .. GENERATED FROM PYTHON SOURCE LINES 41-44 First, we create a pipeline of two transformers: the first one shifts the data while the second one reduces their amplitude. .. GENERATED FROM PYTHON SOURCE LINES 44-46 .. code-block:: Python pipeline = Pipeline(transformers=[Scaler(offset=1), Scaler(coefficient=2)]) .. GENERATED FROM PYTHON SOURCE LINES 47-49 Then, we fit this :class:`.Pipeline` to the data, transform them and compute the Jacobian: .. GENERATED FROM PYTHON SOURCE LINES 49-52 .. code-block:: Python transformed_data = pipeline.fit_transform(data) transformed_jac_data = pipeline.compute_jacobian(data) .. GENERATED FROM PYTHON SOURCE LINES 53-55 Lastly, we can do the same with two scalers: .. GENERATED FROM PYTHON SOURCE LINES 55-63 .. code-block:: Python shifter = Scaler(offset=1) shifted_data = shifter.fit_transform(data) scaler = Scaler(coefficient=2) data_shifted_then_scaled = scaler.fit_transform(shifted_data) jac_shifted_then_scaled = scaler.compute_jacobian( shifted_data ) @ shifter.compute_jacobian(data) .. GENERATED FROM PYTHON SOURCE LINES 64-65 and verify that the results are identical: .. GENERATED FROM PYTHON SOURCE LINES 65-68 .. code-block:: Python assert allclose(transformed_data, data_shifted_then_scaled) assert allclose(transformed_jac_data, jac_shifted_then_scaled) .. GENERATED FROM PYTHON SOURCE LINES 69-71 Note that a :class:`.Pipeline` can compute the Jacobian as long as the :class:`.BaseTransformer` instances that make it up can do so. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.003 seconds) .. _sphx_glr_download_examples_mlearning_transformer_plot_pipeline.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_pipeline.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_pipeline.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_