python – 多维数组上的Numpy直方图
发布时间:2020-05-23 20:08:53 所属栏目:Python 来源:互联网
导读:给定np.array的形状(n_days,n_lat,n_lon),我想计算每个lat-lon单元的固定bin的直方图(即每日值的分布). 解决这个问题的一个简单方法是遍历单元格并为每个单元格调用np.histogram :: bins = np.linspace(0, 1.0, 10)B = np.rand(n_days, n_lat, n_lon)H = np.z
|
给定np.array的形状(n_days,n_lat,n_lon),我想计算每个lat-lon单元的固定bin的直方图(即每日值的分布). 解决这个问题的一个简单方法是遍历单元格并为每个单元格调用np.histogram :: bins = np.linspace(0,1.0,10)
B = np.rand(n_days,n_lon)
H = np.zeros((n_bins,dtype=np.int32)
for lat in range(n_lat):
for lon in range(n_lon):
H[:,lat,lon] = np.histogram(A[:,lon],bins=bins)[0]
# note: code not tested
但这很慢.有没有更有效的解决方案,不涉及循环? 我查看了np.searchsorted来获取B中每个值的bin索引,然后使用花式索引来更新H :: bin_indices = bins.searchsorted(B) H[bin_indices.ravel(),idx[0],idx[1]] += 1 # where idx is a index grid given by np.indices # note: code not tested 但这不起作用,因为就地添加运算符(=)似乎不支持同一单元格的多次更新. 谢谢, 解决方法您可以使用numpy.apply_along_axis来消除循环.hist,bin_edges = apply_along_axis(lambda x: histogram(x,bins=bins),B) (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
