如何用死亡的样本绘制数据?

时间:2017-07-17 20:59:54

标签: python numpy matplotlib

我正在试验一个程序的有效性,该程序在找到解决方案并尝试创建图表以显示程序如何寻找解决方案之前一直运行。该计划有时需要500次尝试,有时需要2000次,我可以证明他们在找到目标之前都能稳定地产生更好更好的答案。我有数百次运行要检查,所以我想看看所有运行的平均值是如何随时间推移的,但是,numpy不允许我平均不同长度的数据。如何才能使每个测试编号可用的数据点平均。

EX:trial1 = [33.4853,32.3958,30.2859,33.2958,30.1049,29.3209]

trial2 = [45.2937,44.2983,42.2839,42.1394,41.2938,39.2936,38.1826,36.2483,39.2632,37.1827,35.9936,32.4837,31.5599,29.3209]

BE = numpy.array([trial1,trial2])

BEave = numpy.average(BE,axis = 0)

我想回来:BEave = [39.3895,38.34705,36.2849,37.7176,35.69935,34.30725,38.1826,36.2483,39.2632,37.1827,35.9936,32.4837,31.5599,29.3209]]

1 个答案:

答案 0 :(得分:0)

您可以创建大量的nans并按相应的最大试验次数填写试验。数组行的其余部分将保留nan。然后使用numpy.nanmean沿垂直轴取平均值。

import numpy as np
import matplotlib.pyplot as plt

trial1 = [33.4853, 32.3958, 30.2859, 33.2958, 30.1049, 29.3209]

trial2 = [45.2937, 44.2983, 42.2839, 42.1394, 41.2938, 39.2936, 38.1826, 
          36.2483, 39.2632, 37.1827, 35.9936, 32.4837, 31.5599, 29.3209]

m= np.max([len(trial1), len(trial2)])

# create array of nans
BE = np.ones( (2, m) )*np.nan 
# fill it with trials up to the number of trial values
BE[0,:len(trial1)] = trial1
BE[1,:len(trial2)] = trial2

# nanmean = take mean, ignore nans
BEave = np.nanmean(BE, axis=0)

plt.plot(trial1, label="trial1", color="mediumpurple")
plt.plot(trial2, label="trial2", color="violet")
plt.plot(BEave, color="crimson", label="avg")

plt.legend()
plt.show()

enter image description here