如何使用python中的2D直方图计算回收箱的平均值?我有x和y轴的温度范围,我试着用回收箱来绘制闪电的概率。我正在读取csv文件中的数据,我的代码如下:
filename = 'Random_Events_All_Sorted_85GHz.csv'
df = pd.read_csv(filename)
min37 = df.min37
min85 = df.min85
verification = df.five_min_1
#Numbers
x = min85
y = min37
H = verification
#Estimate the 2D histogram
nbins = 4
H, xedges, yedges = np.histogram2d(x,y,bins=nbins)
#Rotate and flip H
H = np.rot90(H)
H = np.flipud(H)
#Mask zeros
Hmasked = np.ma.masked_where(H==0,H)
#Plot 2D histogram using pcolor
fig1 = plt.figure()
plt.pcolormesh(xedges,yedges,Hmasked)
plt.xlabel('min 85 GHz PCT (K)')
plt.ylabel('min 37 GHz PCT (K)')
cbar = plt.colorbar()
cbar.ax.set_ylabel('Probability of Lightning (%)')
plt.show()这是一个漂亮的图,但是所绘制的数据是计数,或者每个垃圾箱中的样本数。验证变量是包含1和0的数组,其中1表示闪电,0表示没有闪电。我希望图中的数据是根据验证变量中的数据对给定的垃圾箱进行雷击的概率,因此我需要bin_mean*100来获得这个百分比。
我尝试使用一种类似于这里所示的方法(binning data in python with scipy/numpy),但我很难将它用于2D直方图。
发布于 2014-07-24 08:06:31
至少通过以下方法可以做到这一点
# xedges, yedges as returned by 'histogram2d'
# create an array for the output quantities
avgarr = np.zeros((nbins, nbins))
# determine the X and Y bins each sample coordinate belongs to
xbins = np.digitize(x, xedges[1:-1])
ybins = np.digitize(y, yedges[1:-1])
# calculate the bin sums (note, if you have very many samples, this is more
# effective by using 'bincount', but it requires some index arithmetics
for xb, yb, v in zip(xbins, ybins, verification):
avgarr[yb, xb] += v
# replace 0s in H by NaNs (remove divide-by-zero complaints)
# if you do not have any further use for H after plotting, the
# copy operation is unnecessary, and this will the also take care
# of the masking (NaNs are plotted transparent)
divisor = H.copy()
divisor[divisor==0.0] = np.nan
# calculate the average
avgarr /= divisor
# now 'avgarr' contains the averages (NaNs for no-sample bins)如果您事先知道bin边缘,则只需添加一行就可以在相同的范围内完成直方图部分。
发布于 2015-01-03 00:01:14
有一个优雅和快速的方式来做这件事!使用weights参数求和值:
denominator, xedges, yedges = np.histogram2d(x,y,bins=nbins)
nominator, _, _ = np.histogram2d(x,y,bins=[xedges, yedges], weights=verification)因此,您只需要在每个bin中将值之和除以事件数:
result = nominator / denominator.clip(1)瞧!
https://stackoverflow.com/questions/24917685
复制相似问题