.. 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: Scaler example ============== In this example, we will create a scaler to transform data. .. GENERATED FROM PYTHON SOURCE LINES 27-46 .. code-block:: Python from __future__ import annotations import matplotlib.pyplot as plt from numpy import linspace from numpy import max as npmax from numpy import mean from numpy import min as npmin from numpy import sin from numpy import std from gemseo import configure_logger 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 configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 47-49 Create dataset -------------- .. GENERATED FROM PYTHON SOURCE LINES 49-53 .. code-block:: Python x = linspace(0, 1, 100)[:, None] data = (x < 0.3) * 5 * x + (x > 0.3) * sin(20 * x) .. GENERATED FROM PYTHON SOURCE LINES 54-56 Create transformers ------------------- .. GENERATED FROM PYTHON SOURCE LINES 56-61 .. code-block:: Python same_scaler = Scaler() scaler = Scaler(offset=-2, coefficient=0.5) min_max_scaler = MinMaxScaler() standard_scaler = StandardScaler() .. GENERATED FROM PYTHON SOURCE LINES 62-64 Transform data -------------- .. GENERATED FROM PYTHON SOURCE LINES 64-69 .. code-block:: Python same_data = same_scaler.fit_transform(data) scaled_data = scaler.fit_transform(data) min_max_scaled_data = min_max_scaler.fit_transform(data) standard_scaled_data = standard_scaler.fit_transform(data) .. rst-class:: sphx-glr-script-out .. code-block:: none WARNING - 10:56:15: The Scaler.fit() function does nothing; the instance of Scaler uses the coefficient and offset passed at its initialization WARNING - 10:56:15: The Scaler.fit() function does nothing; the instance of Scaler uses the coefficient and offset passed at its initialization .. GENERATED FROM PYTHON SOURCE LINES 70-72 Compute jacobian ---------------- .. GENERATED FROM PYTHON SOURCE LINES 72-78 .. code-block:: Python jac_same = same_scaler.compute_jacobian(data) jac_scaled = scaler.compute_jacobian(data) jac_min_max_scaled = min_max_scaler.compute_jacobian(data) jac_standard_scaled = standard_scaler.compute_jacobian(data) jac_standard_scaled .. rst-class:: sphx-glr-script-out .. code-block:: none array([[[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]], [[1.42827181]]]) .. GENERATED FROM PYTHON SOURCE LINES 79-87 Print properties ---------------- We may print the min, max, mean and standard deviation of the transformed data. This reveals some of the properties of the different scalers: The scaler without arguments has an offset of 0 and a scaling coefficient of 1, which turns this transformer into the identity function. The min-max scaler has a min of 0 and a max of 1. The standard scaler has a mean of zero and a standard deviation of 1. .. GENERATED FROM PYTHON SOURCE LINES 87-100 .. code-block:: Python names = [ "Original data ", "Same scaler ", "Scaler(-2, 0.5)", "Min-max scaler ", "Standard scaler", ] print("{:^18}{:^8}{:^8}{:^8}{:^8}".format("", "min", "max", "mean", "std")) for name, y in zip( names, [data, same_data, scaled_data, min_max_scaled_data, standard_scaled_data] ): print(f"{name} : {npmin(y): .3f}, {npmax(y): .3f}, {mean(y): .3f}, {std(y): .3f}") .. rst-class:: sphx-glr-script-out .. code-block:: none min max mean std Original data : -0.996, 1.465, 0.251, 0.700 Same scaler : -0.996, 1.465, 0.251, 0.700 Scaler(-2, 0.5) : -2.498, -1.268, -1.874, 0.350 Min-max scaler : 0.000, 1.000, 0.507, 0.285 Standard scaler : -1.782, 1.733, 0.000, 1.000 .. GENERATED FROM PYTHON SOURCE LINES 101-103 Plot data --------- .. GENERATED FROM PYTHON SOURCE LINES 103-110 .. code-block:: Python plt.plot(x, data, label="Original") plt.plot(x, same_data, label="Identity scaled", linestyle="--") plt.plot(x, scaled_data, label="Scaled(-2, 0.5)") plt.plot(x, min_max_scaled_data, label="Min-max") plt.plot(x, standard_scaled_data, label="Standard") plt.legend() 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 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.157 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 `_