Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Normalizing a list of numbers in Python

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] 
like image 748
Adam_G Avatar asked Nov 06 '14 17:11

Adam_G


People also ask

Which library in Python is used for normalizing features?

Python provides the preprocessing library, which contains the normalize function to normalize the data. It takes an array in as an input and normalizes its values between 0 and 1.


1 Answers

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

like image 135
Tony Suffolk 66 Avatar answered Sep 23 '22 21:09

Tony Suffolk 66