Implement inverse of minmax scaler in numpy

Question:

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

Asked By: Talha Anwar

||

Answers:

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]]
Answered By: I'mahdi
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.