Normalizing a list of numbers in Python

前端 未结 9 1211
北荒
北荒 2020-12-05 06:37

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 Pytho

9条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-05 06:59

    If you consider using numpy, you can get a faster solution.

    import random, time
    import numpy as np
    
    a = random.sample(range(1, 20000), 10000)
    since = time.time(); b = [i/sum(a) for i in a]; print(time.time()-since)
    # 0.7956490516662598
    
    since = time.time(); c=np.array(a);d=c/sum(a); print(time.time()-since)
    # 0.001413106918334961
    

提交回复
热议问题