I need to normalize a list of values to fit in a probability distribution, i.e. between 0.0 and 1.0.
I understand how to normalize, but was curious if Python had a function to automate this.
I'd like to go from:
raw = [0.07, 0.14, 0.07]
to
normed = [0.25, 0.50, 0.25]
How long is the list you're going to normalize?
Output
There isn't any function in the standard library (to my knowledge) that will do it, but there are absolutely modules out there which have such functions. However, its easy enough that you can just write your own function:
Sample output:
if your list has negative numbers, this is how you would normalize it
Use :
to normalize against the sum to ensure that the sum is always 1.0 (or as close to as possible).
use
to normalize against the maximum
If you consider using
numpy
, you can get a faster solution.try: