Normalizing a list of numbers in Python

前端 未结 9 1197
北荒
北荒 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 07:16

    Try this :

    from __future__ import division
    
    raw = [0.07, 0.14, 0.07]  
    
    def norm(input_list):
        norm_list = list()
    
        if isinstance(input_list, list):
            sum_list = sum(input_list)
    
            for value in input_list:
                tmp = value  /sum_list
                norm_list.append(tmp) 
    
        return norm_list
    
    print norm(raw)
    

    This will do what you asked. But I will suggest to try Min-Max normalization.

    min-max normalization :

    def min_max_norm(dataset):
        if isinstance(dataset, list):
            norm_list = list()
            min_value = min(dataset)
            max_value = max(dataset)
    
            for value in dataset:
                tmp = (value - min_value) / (max_value - min_value)
                norm_list.append(tmp)
    
        return norm_list
    
    0 讨论(0)
  • 2020-12-05 07:19

    try:

    normed = [i/sum(raw) for i in raw]
    
    normed
    [0.25, 0.5, 0.25]
    
    0 讨论(0)
  • 2020-12-05 07:21

    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:

    def normalize(lst):
        s = sum(lst)
        return map(lambda x: float(x)/s, lst)
    

    Sample output:

    >>> normed = normalize(raw)
    >>> normed
    [0.25, 0.5, 0.25]
    
    0 讨论(0)
提交回复
热议问题