.. 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-41 .. code-block:: default 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() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 42-44 Create dataset -------------- .. GENERATED FROM PYTHON SOURCE LINES 44-48 .. code-block:: default x = linspace(0, 1, 100)[:, None] data = sin(10 * x) - 3 * x .. GENERATED FROM PYTHON SOURCE LINES 49-55 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 55-59 .. code-block:: default shift = Scaler(offset=5) scale = Scaler(coefficient=0.5) pipeline = Pipeline(transformers=[shift, scale]) .. GENERATED FROM PYTHON SOURCE LINES 60-63 Transform data -------------- In order to use the transformer, we have to fit it to the data. .. GENERATED FROM PYTHON SOURCE LINES 63-65 .. code-block:: default pipeline.fit(data) .. rst-class:: sphx-glr-script-out .. code-block:: none WARNING - 13:54:02: The Scaler.fit() function does nothing; the instance of Scaler uses the coefficient and offset passed at its initialization WARNING - 13:54:02: The Scaler.fit() function does nothing; the instance of Scaler uses the coefficient and offset passed at its initialization .. GENERATED FROM PYTHON SOURCE LINES 66-67 Transform data using the pipeline .. GENERATED FROM PYTHON SOURCE LINES 67-69 .. code-block:: default transformed_data = pipeline.transform(data) .. GENERATED FROM PYTHON SOURCE LINES 70-71 Transform data using individual components of the pipeline .. GENERATED FROM PYTHON SOURCE LINES 71-73 .. code-block:: default only_shifted_data = shift.transform(data) .. GENERATED FROM PYTHON SOURCE LINES 74-76 Plot data --------- .. GENERATED FROM PYTHON SOURCE LINES 76-82 .. code-block:: default 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 83-85 Compute jacobian ---------------- .. GENERATED FROM PYTHON SOURCE LINES 85-93 .. code-block:: default 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.140 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-python :download:`Download Python source code: plot_pipeline.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_pipeline.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_