1

I have a numpy array:

a = np.arange(500).reshape(100,5)

I can normalize it using the function below:

def normalizer(X, mini, maxi):
    X_std = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))
    X_scaled = X_std * (maxi - mini) + mini
    return X_scaled

normalized = normalizer(X, -1, +1)

Now, I want to denormalize it, I mean get original array. What function should I write?

 def denormalizer():

denormalized = denormalizer()
2
  • Normalizing sets the value of some property to 1. In your case it appears that you use the max row value. You need to record the max and min values in a row and reverse what you did. This seems odd, since you are just recreating what you had as an input. Commented May 1, 2018 at 3:48
  • yes, I want to recreate by knowing how to reversing it. Commented May 1, 2018 at 3:51

1 Answer 1

2

I would agree in my answer with @Matt. One thing you can do is save all parameters of the normalisation (X_min, X_max, mini, maxi) and reverse all mathematical operations, for example:

 def normalizer(X, mini, maxi):                         
     X_min = X.min(axis=0)                                  
     X_max = X.max(axis=0)               
     X_std = (X - X_min) / (X_max - X_min)
     X_scaled = X_std * (maxi - mini) + mini                
     return X_scaled, {'x_min': X_min, 
                       'x_max': X_max, 
                       'min': mini, 
                       'max': maxi}


 def denormalizer(X_scaled, params):
     X_min = params['x_min']
     X_max = params['x_max']
     mini = params['min']
     maxi = params['max']
     X_std = (X_scaled - mini) / (maxi - mini)           
     X = X_std * (X_max - X_min) + X_min 
     return X


a = np.arange(500).reshape(100,5)
a_scaled, params = normalizer(a, -1, 1)
a_restored = denormalizer(a_scaled, params)
print(a - a_restored)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.