Home > Software design >  Is the np.clip redundant for the min-max normalization
Is the np.clip redundant for the min-max normalization

Time:08-12

What's the range of the default min-max normalization? Is it [0, 1] or [−1, 1] or both? How to get the range of [-1,1]?

X_scaled = (X - X_min)/(X_max - X_min)

And the numpy clip:

X_scaled_clipped = np.clip((X - X_min)/(X_max - X_min), 0, 1)

Is the np.clip(..., 0, 1) redundant here?

CodePudding user response:

This transformation:

def minmax_xform(x):
    x_max = np.amax(x)
    x_min = np.amin(x)
    return (x - x_min) / (x_max - x_min)

Gives you values in the [0, 1] range.

To get to the [-1, 1] range, just multiply by 2 (the size of the new range -- the new maximum minus the new minimum) and add -1 (the new minimum):

def minmax_xform(x, max_=1, min_=-1):
    x_max = np.amax(x)
    x_min = np.amin(x)
    size = (max_ - min_)
    return size * (x - x_min) / (x_max - x_min)   min_

The clipping would be redundant.

Note that all this assumes no NaNs or infs, which should be addressed separately.

  • Related