非有序列表中相对概率的累计和

时间:2017-11-12 16:10:37

标签: python python-3.x probability cumulative-sum

population_d = {'0,0,1,0,1,1,0,1,1,1,1,0,0,0,0,1': 6, 
'0,0,1,1,1,0,0,1,1,0,1,1,0,0,0,1': 3, 
'0,1,1,0,1,1,0,0,1,1,1,0,0,1,0,0': 5, 
'1,0,0,1,1,1,0,0,1,1,0,1,1,0,0,0': 1}

def ProbabilityList(population_d):
    fitness = population_d.values()
    total_fit = (sum(fitness))
    relative_fitness = [f/total_fit for f in fitness]
    probabilities = [sum(relative_fitness[:i+1]) for i in range(len(relative_fitness))]
    return (probabilities)

我试图计算这种数据结构的累积概率,但是,我需要保持值的顺序,以便将它们索引到另一个列表中相同位置的相应个体。

程序按顺序执行操作,为最后一个位置提供更高的权重,在这种情况下,这是最低的适应度。

有没有人知道是否有办法以正确的方式(健身值的新月顺序)执行累积和,而不改变它们在输出列表中的位置?

非常感谢!

1 个答案:

答案 0 :(得分:1)

population_d = {'0,0,1,0,1,1,0,1,1,1,1,0,0,0,0,1': 6, 
                '0,0,1,1,1,0,0,1,1,0,1,1,0,0,0,1': 3, 
                '0,1,1,0,1,1,0,0,1,1,1,0,0,1,0,0': 5, 
                '1,0,0,1,1,1,0,0,1,1,0,1,1,0,0,0': 1}

在词典中,您将健身(?)值与唯一标识符相关联。据推测,这些标识符来自程序和数据集中的其他位置。我没有试图依赖字典的构造顺序来保持这种关系,而是维持了这种关联,并构建了一个新的字典,其值是将健身从低到高排序后得到的累积和。

import operator
def ProbabilityList(population_d):
    fitness = population_d.values()
    total_fit = (sum(fitness))

    #create list of (individual, fitness) tuples
    items = population_d.items()

    #sort by fitness value
    items = sorted(items, key = operator.itemgetter(1))
    #some people prefer
    #items = sorted(items, key = lambda item: item[1])
    #print(items)

    #maintain association and calculate relative fitness
    relative_fitness = [(ind,fit/total_fit) for (ind,fit) in items]
    #print(relative_fitness)

    cumsum = 0
    probabilities = {}
    for ind, fit in relative_fitness:
        cumsum += fit
        probabilities[ind] = cumsum
    return (probabilities)

d = ProbabilityList(population_d)
for k, v in d.items():
    print('key:{}, fitness:{}, cumsum:{}'.format(k, population_d[k], v))

>>>
key:1,0,0,1,1,1,0,0,1,1,0,1,1,0,0,0, fitness:1, cumsum:0.06666666666666667
key:0,0,1,1,1,0,0,1,1,0,1,1,0,0,0,1, fitness:3, cumsum:0.26666666666666666
key:0,1,1,0,1,1,0,0,1,1,1,0,0,1,0,0, fitness:5, cumsum:0.6
key:0,0,1,0,1,1,0,1,1,1,1,0,0,0,0,1, fitness:6, cumsum:1.0
>>>

希望通过字典,您可以将累积金额与代码其他部分中的原始个人联系起来。

我发现您一直在询问与此数据集和项目相关的其他问题。您可能希望花一些时间学习Pandas,或者甚至考虑将数据保存在数据库中,而不是分散在整个项目中的单个容器。