[英]Is there a way to vectorize applying the mean function to masked regions in an ndarray?
Let's say I have a two ndarray define as such:假设我有两个 ndarray 定义如下:
import numpy as np
mask = np.array([[1,1],[1,2]])
values = np.array([[1., 3.],[2., 2.]])
My goal is to calculate the mean of the values based on the mask regions indicated by the integer in mask
.我的目标是根据掩码中的 integer 指示的
mask
区域计算值的平均值。 Naturally, I would use a for-loop:自然,我会使用 for 循环:
out = np.zeros(len(np.unique(mask)))
for j,i in enumerate(np.unique(mask)):
out[j] = np.nanmean(values[mask==i])
However, this serialized solution becomes very slow for large, multidimensional arrays.但是,对于大型多维 arrays,此序列化解决方案变得非常缓慢。 Is there a way to vectorize this operation efficiently?
有没有办法有效地矢量化这个操作? Thank you for your help in advance!
提前谢谢你的帮助!
You can use np.bincount
:您可以使用
np.bincount
:
unq,inv,cnt = np.unique(mask,return_inverse=1,return_counts=1)
np.bincount(inv,values.ravel())/cnt
# array([2., 2.])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.