Is the np.clip redundant for the min-max normalization

Question:

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?

Asked By: stackbiz

||

Answers:

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.

Answered By: norok2
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.