pythonnormalizationnormalize

Normalize a list containing only positive data into a range comprising negative and positive values


I have a list of data that only contains positive values, such as the list below:

data_list = [3.34, 2.16, 8.64, 4.41, 5.0]

Is there a way to normalize this list into a range that spans from -1 to +1?

The value 2.16 should correspond to -1 in the normalized range, and the value 8.64 should correspond to +1.

I found several topics treating the question of how one can normalize a list that contains negative and positive values. But how can one normalize a list of only positive or only negative values into a new normalized list that spans from the negative into the positive range?


Solution

  • While there are ready-made lib functions available, you can very easily replicate that logic with a simple list comprehension.

    data_list = [3.34, 2.16, 8.64, 4.41, 5.0]
    
    minL = min(data_list)
    maxL = max(data_list)
    
    normalised = [((x - minL) * 2)/(maxL - minL) - 1 for x in data_list]
    
    print(normalised) # [-0.6358024691358026, -1.0, 1.0, -0.3055555555555556, -0.1234567901234569]