简体   繁体   English

如何计算每次迭代的平均值?

[英]How to calculate average for each iteration?

I want to calculate an average of fitness candidate in each iteration but I don't know how to do it.我想在每次迭代中计算适合度候选者的平均值,但我不知道该怎么做。

import pandas as pd
import numpy as np

  while iteration < n_iterations:
        print('iteration     fitness_candidate')
        for i in range(n_particles):

            temp = []
            fitness_cadidate = fitness_function(particle_position_vector[i])
            print(iteration,' ', -(fitness_cadidate))

            temp.append(iteration)
            temp.append(particle_position_vector[i])
            temp.append(-(fitness_cadidate))
            ls.append(temp)

        iteration = iteration + 1

ls = pd.DataFrame(ls)

As you can see the each iteration generate several fitness candidate.如您所见,每次迭代都会生成几个适应度候选者。 So I need to calculate the average of fitness candidate within the iteration only.所以我只需要在迭代中计算适应度候选者的平均值。 If it have 4 iteration, so it need to generate 4 average value.如果有 4 次迭代,则需要生成 4 个平均值。

output:输出:

iteration     fitness_candidate
0            20.24475
0            15.720000000000002
0            16.242250000000002
0            11.0975
0            20.923250000000007
0            15.720000000000002
0            22.924500000000002
0            17.472250000000003
0            24.247250000000005
0            24.305750000000003
iteration     fitness_candidate
1            21.72342
1            16.798420000000004
1            19.321920000000002
1            10.945920000000001
1            21.601420000000008
1            17.598920000000003
1            23.202420000000007
1            20.55192
1            24.124920000000003
1            24.305750000000003
iteration     fitness_candidate
2            22.801840000000002
2            19.47784
2            21.601090000000003
2            15.597339999999999
2            22.279590000000002
2            19.878089999999997
2            23.080090000000002
2            22.152920000000005
2            24.402840000000005
2            24.305750000000003
iteration     fitness_candidate
3            23.050510000000006
3            20.52701
3            21.44951
3            17.447010000000002
3            22.12801
3            19.72651
3            22.528260000000003
3            22.001340000000003
3            24.402840000000005
3            24.00259

You may use:您可以使用:

while iteration < n_iterations:
    print('iteration     fitness_candidate')
    for i in range(n_particles):
        print(iteration,' ', -(fitness_cadidate)

    print("Average",' ', sum([-(fitness_function(particle_position_vector[i])) for i in range(n_particles)])/len(n_particles))
    iteration = iteration + 1

If you have a loop, python list comprehension allows you to directly dump the results into a list [i for i in data] .如果你有一个循环,python list comprehension 允许你直接将结果转储到一个列表[i for i in data] This means we can apply numpys mean function to the said list and get a result.这意味着我们可以将 numpys mean 函数应用于所述列表并获得结果。 If you want a list of results, we can add these to a new list ( results ) on each iteration cycle.如果您需要结果列表,我们可以在每个迭代周期将它们添加到新列表 ( results ) 中。

import numpy as np

results =[]

while iteration < n_iterations:
    print('iteration     fitness_candidate')

    mean = np.mean( [-(fitness_cadidate) for i in range(n_particles)] )

    print(iteration,mean)
    results.append(mean)

    iteration = iteration + 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM