.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/mlearning/transformer/plot_scaler.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_scaler.py: Scalers ======= .. GENERATED FROM PYTHON SOURCE LINES 25-35 .. code-block:: Python from __future__ import annotations import matplotlib.pyplot as plt from numpy import linspace from gemseo.mlearning.transformers.scaler.min_max_scaler import MinMaxScaler from gemseo.mlearning.transformers.scaler.scaler import Scaler from gemseo.mlearning.transformers.scaler.standard_scaler import StandardScaler .. GENERATED FROM PYTHON SOURCE LINES 36-39 Scaling data may be important, as discussed in another example. Different scalers are available and this example illustrate them with these simple data: .. GENERATED FROM PYTHON SOURCE LINES 39-41 .. code-block:: Python data = linspace(-2, 2, 100) .. GENERATED FROM PYTHON SOURCE LINES 42-46 First, a :class:`.Scaler` transforms a value :math:`x` into a new value :math:`\tilde{x}` based on the linear function :math:`\tilde{x}=a+bx`. By default, the offset :math:`a` is zero and the coefficient :math:`b` is one: .. GENERATED FROM PYTHON SOURCE LINES 46-48 .. code-block:: Python default_scaler = Scaler() .. GENERATED FROM PYTHON SOURCE LINES 49-50 We can set these coefficient and offset at instantiation: .. GENERATED FROM PYTHON SOURCE LINES 50-52 .. code-block:: Python custom_scaler = Scaler(offset=-1, coefficient=0.5) .. GENERATED FROM PYTHON SOURCE LINES 53-55 or use a specific :class:`.Scaler` for that, e.g. a :class:`.MinMaxScaler`: .. GENERATED FROM PYTHON SOURCE LINES 55-57 .. code-block:: Python min_max_scaler = MinMaxScaler() .. GENERATED FROM PYTHON SOURCE LINES 58-59 or a :class:`.StandardScaler`: .. GENERATED FROM PYTHON SOURCE LINES 59-61 .. code-block:: Python standard_scaler = StandardScaler() .. GENERATED FROM PYTHON SOURCE LINES 62-64 In this case, the coefficient and offset will be computed from ``data``. .. GENERATED FROM PYTHON SOURCE LINES 66-68 Now, we fit each scaler from ``data`` and transform these ``data``: .. GENERATED FROM PYTHON SOURCE LINES 68-83 .. code-block:: Python same_data = default_scaler.fit_transform(data) scaled_data = custom_scaler.fit_transform(data) min_max_scaled_data = min_max_scaler.fit_transform(data) standard_scaled_data = standard_scaler.fit_transform(data) # # We can plot the transformed data versus the original one: plt.plot(data, default_scaler.fit_transform(data), label="Default scaler") plt.plot(data, custom_scaler.fit_transform(data), label="Custom scaler") plt.plot(data, min_max_scaler.fit_transform(data), label="Min-max scaler") plt.plot(data, standard_scaler.fit_transform(data), label="Standard scaler") plt.legend() plt.grid() plt.show() .. image-sg:: /examples/mlearning/transformer/images/sphx_glr_plot_scaler_001.png :alt: plot scaler :srcset: /examples/mlearning/transformer/images/sphx_glr_plot_scaler_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 84-94 The specific features of the different scalers are clearly visible. In particular, the :class:`.MinMaxScaler` projects the data onto the interval :math:`[0,1]` as long as this data is included in the fitting interval. The :class:`.StandardScaler` guarantees that the transformed ``data`` have zero mean and unit variance. Lastly, every scaler can compute the Jacobian, e.g. .. GENERATED FROM PYTHON SOURCE LINES 94-95 .. code-block:: Python custom_scaler.compute_jacobian(data) .. rst-class:: sphx-glr-script-out .. code-block:: none array([[0.5]]) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.159 seconds) .. _sphx_glr_download_examples_mlearning_transformer_plot_scaler.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_scaler.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_scaler.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_