.. 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 Click :ref:`here ` 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 29-41 .. code-block:: default from __future__ import division, unicode_literals import matplotlib.pyplot as plt 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() .. rst-class:: sphx-glr-script-out 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) 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-71 .. code-block:: default 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) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none WARNING - 12:57:07: The Scaler.fit() function does nothing; the instance of Scaler uses the coefficient and offset passed at its initialization WARNING - 12:57:07: The Scaler.fit() function does nothing; the instance of Scaler uses the coefficient and offset passed at its initialization .. GENERATED FROM PYTHON SOURCE LINES 72-74 Plot data --------- .. GENERATED FROM PYTHON SOURCE LINES 74-80 .. 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:: /examples/mlearning/transformer/images/sphx_glr_plot_pipeline_001.png :alt: plot pipeline :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 81-83 Compute jacobian ---------------- .. GENERATED FROM PYTHON SOURCE LINES 83-91 .. 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) print(only_shift_jac) print(only_scale_jac) print(allclose(jac, matmul(only_scale_jac, only_shift_jac))) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [[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 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.151 seconds) .. _sphx_glr_download_examples_mlearning_transformer_plot_pipeline.py: .. only :: html .. container:: sphx-glr-footer :class: 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 `_