.. 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: Transformer pipeline example ============================ In this example, we will create a pipeline of transformers. .. GENERATED FROM PYTHON SOURCE LINES 27-43 .. code-block:: Python from __future__ import annotations import matplotlib.pyplot as plt from numpy import allclose from numpy import linspace from numpy import matmul from numpy import sin from gemseo import configure_logger from gemseo.mlearning.transformers.pipeline import Pipeline from gemseo.mlearning.transformers.scaler.scaler import Scaler configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 44-46 Create dataset -------------- .. GENERATED FROM PYTHON SOURCE LINES 46-50 .. code-block:: Python x = linspace(0, 1, 100)[:, None] data = sin(10 * x) - 3 * x .. GENERATED FROM PYTHON SOURCE LINES 51-57 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. .. GENERATED FROM PYTHON SOURCE LINES 57-61 .. code-block:: Python shift = Scaler(offset=5) scale = Scaler(coefficient=0.5) pipeline = Pipeline(transformers=[shift, scale]) .. GENERATED FROM PYTHON SOURCE LINES 62-65 Transform data -------------- In order to use the transformer, we have to fit it to the data. .. GENERATED FROM PYTHON SOURCE LINES 65-67 .. code-block:: Python pipeline.fit(data) .. rst-class:: sphx-glr-script-out .. code-block:: none WARNING - 10:54:08: The Scaler.fit() function does nothing; the instance of Scaler uses the coefficient and offset passed at its initialization WARNING - 10:54:08: The Scaler.fit() function does nothing; the instance of Scaler uses the coefficient and offset passed at its initialization .. GENERATED FROM PYTHON SOURCE LINES 68-69 Transform data using the pipeline .. GENERATED FROM PYTHON SOURCE LINES 69-71 .. code-block:: Python transformed_data = pipeline.transform(data) .. GENERATED FROM PYTHON SOURCE LINES 72-73 Transform data using individual components of the pipeline .. GENERATED FROM PYTHON SOURCE LINES 73-75 .. code-block:: Python only_shifted_data = shift.transform(data) .. GENERATED FROM PYTHON SOURCE LINES 76-78 Plot data --------- .. GENERATED FROM PYTHON SOURCE LINES 78-84 .. code-block:: Python 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() .. image-sg:: /examples/mlearning/transformer/images/sphx_glr_plot_pipeline_001.png :alt: plot pipeline :srcset: /examples/mlearning/transformer/images/sphx_glr_plot_pipeline_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 85-87 Compute jacobian ---------------- .. GENERATED FROM PYTHON SOURCE LINES 87-95 .. code-block:: Python 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))) .. rst-class:: sphx-glr-script-out .. code-block:: none (100, 1, 1) (100, 1, 1) (100, 1, 1) True .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.139 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 `_