Comment inverser la normalisation ("data scaling") d'une variable avec scikit learn en python ?

Active 14 juillet 2020    /    Viewed 1041    /    Comments 0    /    Edit


Pour inverser la normalisation ("data scaling") d'une variable avec scikit-learn en python, une solution est d'utiliser inverse_transform(), exemple

Summary

Soit les données suivantes:

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)

donne

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

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)

donne

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

Calculer la moyenne:

print(x.mean())

-9.081254267092239e-13

Calculer la standard déviation:

print(x.std())

1.0

Revenir aux données initiales:

x = scaler.inverse_transform(x)

print(x)

donne

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

Références


Card image cap
profile-image
Daidalos

Hi, I am Ben.

I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!