如何在 Numpy 中创建具有掩码值的数组的直方图?
在 Numpy 1.4.1 中,计算掩码数组的直方图最简单或最有效的方法是什么?默认情况下,numpy.histogram
和 pyplot.hist
会计算屏蔽元素!
我现在能想到的唯一简单的解决方案涉及使用非屏蔽值创建一个新数组:
histogram(m_arr[~m_arr.mask])
但这不是很有效,因为这不必要地创建一个新数组。我很乐意阅读更好的想法!
In Numpy 1.4.1, what is the simplest or most efficient way of calculating the histogram of a masked array? numpy.histogram
and pyplot.hist
do count the masked elements, by default!
The only simple solution I can think of right now involves creating a new array with the non-masked value:
histogram(m_arr[~m_arr.mask])
This is not very efficient, though, as this unnecessarily creates a new array. I'd be happy to read about better ideas!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
(根据上面的讨论取消删除它......)
我不确定 numpy 开发人员是否会认为这是一个错误或预期行为。我在邮件列表上询问,所以我想我们看看他们怎么说。
无论哪种方式,这都是一个简单的修复。修补
numpy/lib/function_base.py
以在函数的输入上使用numpy.asanyarray
而不是numpy.asarray
将允许它正确地使用屏蔽数组(或 ndarray 的任何其他子类)而不创建副本。编辑:这似乎是预期的行为。 正如此处讨论的:
(Undeleting this as per discussion above...)
I'm not sure whether or not the numpy developers would consider this a bug or expected behavior. I asked on the mailing list, so I guess we'll see what they say.
Either way, it's an easy fix. Patching
numpy/lib/function_base.py
to usenumpy.asanyarray
rather thannumpy.asarray
on the inputs to the function will allow it to properly use masked arrays (or any other subclass of an ndarray) without creating a copy.Edit: It seems like it is expected behavior. As discussed here:
尝试
hist(m_arr.compressed())
。Try
hist(m_arr.compressed())
.这是一个超级老的问题,但现在我只是使用:
numpy.histogram(m_arr, bins=.., range=.., Density=False, Weights=m_arr_mask)
其中 m_arr_mask 是一个数组与 m_arr 具有相同的形状,由要从直方图中排除的 m_arr 元素的 0 值和要包含的元素的 1 值组成。
This is a super old question, but these days I just use:
numpy.histogram(m_arr, bins=.., range=.., density=False, weights=m_arr_mask)
Where m_arr_mask is an array with the same shape as m_arr, consisting of 0 values for elements of m_arr to be excluded from the histogram and 1 values for elements that are to be included.
通过尝试 Erik 的解决方案遇到转换问题后(请参阅 https://github.com/numpy/numpy /issues/16616),我决定编写一个 numba 函数来实现此行为。
一些代码的灵感来自 https://numba.pydata .org/numba-examples/examples/densis_estimation/histogram/results.html。我添加了
mask
位。加速是显着的。在 (1000, 1000) 图像上:
After running into casting issues by trying Erik's solution (see https://github.com/numpy/numpy/issues/16616), I decided to write a numba function to achieve this behavior.
Some of the code was inspired by https://numba.pydata.org/numba-examples/examples/density_estimation/histogram/results.html. I added the
mask
bit.The speedup is significant. On a (1000, 1000) image: