scale()#

scale(states: ndarray, scale_to: tuple, scale_from: tuple = None)[source]#

Scale the entries of a snapshot matrix to a specified interval.

The scaling from the interval \([a, b]\) to the interval \([a', b']\) given by

\[q' = \frac{q - a}{b - a}(b' - a') + a',\]

where \(q\) is the original variable and \(q'\) is the transformed variable. This follows sklearn.preprocessing.MinMaxScaler.

Parameters
states(n, k) ndarray

Matrix of k snapshots to be scaled. Each column is a single snapshot.

scale_to(float, float)

Desired minimum and maximum of the scaled data, i.e., \([a', b']\).

scale_from(float, float)

Minimum and maximum of the snapshot data, i.e., \([a, b]\). If None (default), learn the scaling from the data: scale_from[0] = min(states); scale_from[1] = max(states).

Returns
states_scaled(n, k) ndarray

Scaled snapshot matrix.

scaled_to(float, float)

Bounds that the snapshot matrix was scaled to, i.e., scaled_to[0] = min(states_scaled); scaled_to[1] = max(states_scaled). Only returned if scale_from = None.

scaled_from(float, float)

Minimum and maximum of the snapshot data, i.e., the bounds that the data was scaled from. Only returned if scale_from = None.

Examples

>>> import opinf
# Scale Q to [-1, 1] and then scale Y with the same transformation.
>>> Qscaled, scaled_to, scaled_from = opinf.pre.scale(Q, (-1, 1))
>>> Yscaled = opinf.pre.scale(Y, scaled_to, scaled_from)
# Scale Q to [0, 1], then undo the transformation by an inverse scaling.
>>> Qscaled, scaled_to, scaled_from = opinf.pre.scale(Q, (0, 1))
>>> Q_again = opinf.pre.scale(Qscaled, scaled_from, scaled_to)