Normalising data to [-1 and 1] , but 0 value needs to be preserved

Viewed 717

I have a range of r=data which is both positive and negative. I wanted to normalize it to the [-1,1] range.

I used apply(lambda x: -1 + (2*((x - x.min()) / (x.max() - x.min())))) which normalised the data from -1 to 1, but in my data 0 is significant and thus I would like to needs to be preserved it.

How can I accomplish it ?

2 Answers

Using numpy you can do this

Input:

r =(-5,-10,0,1,17)

import numpy as np
normalized = np.where(r>0,r/r.max(),np.where(r<0,-r/r.min(),r))

Output:

normalized = (-0.5 ,-1 ,0 ,0.0588235,1 )

I think that you are expecting this kind of result

What you are doing is taking your min and max and assuming that this is range that you work within. Then you squeeze all values and shift them so that they fit into [-1, 1] range.

The problem is: when extremums are on a different distance from 0, you will have to shift and that will shift all zeros. This is correct mathematically as it preserves distribution.

Say, for sequence [-1, 0, 2] extremums are -1 and 2. After manipulations, it will become [-1, -0.66, 1].

To fight this, you have to assume that the range you work within is at equal distance from zero on both sides. So that would be your max extremum. In the example above that is 2. Now if you assume that you work within [-2, 2] range, your sequence will become [-0.5, 0, 1].

This is easily achieved simply by dividing all of the items by max extremum.

def normalize(seq):
    extremum = max(abs(i) for i in seq)
    return [i / extremum for i in seq]
Related