python - 如何在 Numpy 中创建具有屏蔽值的数组的直方图?

标签 python arrays numpy histogram

在 Numpy 1.4.1 中,计算掩码数组直方图的最简单或最有效的方法是什么?默认情况下,numpy.histogrampyplot.hist 会计算屏蔽的元素!

我现在能想到的唯一简单的解决方案是创建一个具有非屏蔽值的新数组:

histogram(m_arr[~m_arr.mask])

但这不是很有效,因为这会不必要地创建一个新数组。我很乐意阅读更好的想法!

最佳答案

(根据上面的讨论取消删除...)

我不确定 numpy 开发人员是否会认为这是一个错误或预期的行为。我 asked on the mailing list ,所以我想我们会看看他们怎么说。

无论哪种方式,这都很容易解决。修补 numpy/lib/function_base.py 以在函数的输入上使用 numpy.asanyarray 而不是 numpy.asarray 将允许它正确地使用掩码数组(或 ndarray 的任何其他子类)而不创建副本。

编辑:这似乎是预期的行为。 As discussed here :

If you want to ignore masked data it's just on extra function call

histogram(m_arr.compressed())

I don't think the fact that this makes an extra copy will be relevant, because I guess full masked array handling inside histogram will be a lot more expensive.

Using asanyarray would also allow matrices in and other subtypes that might not be handled correctly by the histogram calculations.

For anything else besides dropping masked observations, it would be necessary to figure out what the masked array definition of a histogram is, as Bruce pointed out.

关于python - 如何在 Numpy 中创建具有屏蔽值的数组的直方图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3610040/

相关文章:

python - 从 MySQL 查询设置 CheckListBox

javascript - 在 Mongoose 中一次执行多个查询

javascript - 带有标题和子标题的代码

C - 初始化指针数组

python - 使用 Python/Numpy 将数据拟合到多项式曲线

python - 将压缩对象放入字典

python - 如何将列表转换为使用元组作为键的字典

python - 使用 Python unittest,如何创建和使用 "callable object that returns a test suite"?

python - 使用 %s 格式化输入/输出

python - 仅保留 numpy 字符串数组中的数字信息