Scaler example

In this example, we will create a scaler to transform data.

from __future__ import annotations

import matplotlib.pyplot as plt
from gemseo.api import configure_logger
from gemseo.mlearning.transform.scaler.min_max_scaler import MinMaxScaler
from gemseo.mlearning.transform.scaler.scaler import Scaler
from gemseo.mlearning.transform.scaler.standard_scaler import StandardScaler
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

configure_logger()
<RootLogger root (INFO)>

Create dataset

x = linspace(0, 1, 100)
data = (x < 0.3) * 5 * x + (x > 0.3) * sin(20 * x)

Create transformers

same_scaler = Scaler()
scaler = Scaler(offset=-2, coefficient=0.5)
min_max_scaler = MinMaxScaler()
standard_scaler = StandardScaler()

Transform data

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)
 WARNING - 11:23:01: The Scaler.fit() function does nothing; the instance of Scaler uses the coefficient and offset passed at its initialization
 WARNING - 11:23:01: The Scaler.fit() function does nothing; the instance of Scaler uses the coefficient and offset passed at its initialization
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/envs/develop/lib/python3.9/site-packages/gemseo/mlearning/transform/scaler/standard_scaler.py:75: RuntimeWarning: divide by zero encountered in divide
  self.coefficient = where(is_constant, nan_to_num(1 / data[0]), 1.0 / std_)

Compute jacobian

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)

print(jac_standard_scaled)
[[1.42827181]]

Plot data

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()
plot scaler

Total running time of the script: ( 0 minutes 0.153 seconds)

Gallery generated by Sphinx-Gallery