Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Implement inverse of minmax scaler in numpy

I want to implement inverse of min-max scaler in numpy instead of sklearn.
Applying min max scale is easy

v_min, v_max = v.min(), v.max()
new_min, new_max = 0, 1
v_p = (v - v_min)/(v_max - v_min)*(new_max - new_min) + new_min
v_p.min(), v_p.max()

But once I got the scaled value, how can i go back to original one in numpy

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try Mathematic:

import numpy as np

org_arr = np.array([
    [2.0, 3.0],
    [2.5, 1.5],
    [0.5, 3.5]
])

# save min & max
min_val = org_arr.min(axis = 0)
max_val = org_arr.max(axis = 0)


scl_arr = (org_arr - min_val) / (max_val - min_val)
print(scl_arr)

# inverse of min-max scaler in numpy
org_arr_2 = scl_arr*(max_val - min_val) + min_val
print(org_arr_2)

Output:

# scl_arr
[[0.75 0.75]
 [1.   0.  ]
 [0.   1.  ]]

# org_arr_2
[[2.  3. ]
 [2.5 1.5]
 [0.5 3.5]]

Check with sklearn.preprocessing.MinMaxScale

from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()

scl_arr = scaler.fit_transform(org_arr)
print(scl_arr)

org_arr_2 = scaler.inverse_transform(scl_arr)
print(org_arr_2)

Output:

# scl_arr
[[0.75 0.75]
 [1.   0.  ]
 [0.   1.  ]]

# org_arr_2
[[2.  3. ]
 [2.5 1.5]
 [0.5 3.5]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading