Normalizing a list of numbers in Python Normalizing a list of numbers in Python python python

Normalizing a list of numbers in Python


Use :

norm = [float(i)/sum(raw) for i in raw]

to normalize against the sum to ensure that the sum is always 1.0 (or as close to as possible).

use

norm = [float(i)/max(raw) for i in raw]

to normalize against the maximum


if your list has negative numbers, this is how you would normalize it

a = range(-30,31,5)norm = [(float(i)-min(a))/(max(a)-min(a)) for i in a]


try:

normed = [i/sum(raw) for i in raw]normed[0.25, 0.5, 0.25]