How to reverse the data scaling applied to a variable with scikit learn in python ?

Published: July 14, 2020

Tags: Python; Scikit-learn; Machine learning;

DMCA.com Protection Status

To reverse the data scaling applied to a variable with scikit learn in python, a solution is to use inverse_transform(), example

Input data

Let's consider the following data:

from sklearn import preprocessing

x = np.array([0.9995577,  0.999717,   0.9997348,  0.99975765, 0.99978703, 0.99980724, 0.9998182,  0.99982077, 0.99981844, 0.99981105, 0.99980015, 0.9997869 ])
x = x[:, np.newaxis]

print(x)

returns

[[0.9995577 ]
 [0.999717  ]
 [0.9997348 ]
 [0.99975765]
 [0.99978703]
 [0.99980724]
 [0.9998182 ]
 [0.99982077]
 [0.99981844]
 [0.99981105]
 [0.99980015]
 [0.9997869 ]]

Data scaling

Pour normaliser les données on peut utiliser le module scikit-learn preprocessing avec StandardScaler:

scaler = preprocessing.StandardScaler().fit(x)
x = scaler.transform(x)

print(x)

returns

[[-2.94994187]
 [-0.71621564]
 [-0.46662162]
 [-0.14621582]
 [ 0.26575453]
 [ 0.54914189]
 [ 0.7028245 ]
 [ 0.73886139]
 [ 0.70618981]
 [ 0.60256623]
 [ 0.44972495]
 [ 0.26393165]]

Calculate the mean

print(x.mean())

-9.081254267092239e-13

Calculate the standard deviation

print(x.std())

1.0

Reverse variable data scaling

Going back to the initial data scale:

x = scaler.inverse_transform(x)

print(x)

returns

[[0.9995577 ]
 [0.999717  ]
 [0.9997348 ]
 [0.99975765]
 [0.99978703]
 [0.99980724]
 [0.9998182 ]
 [0.99982077]
 [0.99981844]
 [0.99981105]
 [0.99980015]
 [0.9997869 ]]

References