首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用histogram2d python查找平均值bin值

使用histogram2d python查找平均值bin值
EN

Stack Overflow用户
提问于 2014-07-23 18:03:00
回答 2查看 7.4K关注 0票数 5

如何使用python中的2D直方图计算回收箱的平均值?我有x和y轴的温度范围,我试着用回收箱来绘制闪电的概率。我正在读取csv文件中的数据,我的代码如下:

代码语言:javascript
复制
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直方图。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-07-24 08:06:31

至少通过以下方法可以做到这一点

代码语言:javascript
复制
# 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边缘,则只需添加一行就可以在相同的范围内完成直方图部分。

票数 1
EN

Stack Overflow用户

发布于 2015-01-03 00:01:14

有一个优雅和快速的方式来做这件事!使用weights参数求和值:

代码语言:javascript
复制
denominator, xedges, yedges = np.histogram2d(x,y,bins=nbins)
nominator, _, _ = np.histogram2d(x,y,bins=[xedges, yedges], weights=verification)

因此,您只需要在每个bin中将值之和除以事件数:

代码语言:javascript
复制
result = nominator / denominator.clip(1)

瞧!

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24917685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档