如何创建蒙面值的数组的直方图,在numpy的? [英] How to create the histogram of an array with masked values, in Numpy?

查看:216
本文介绍了如何创建蒙面值的数组的直方图,在numpy的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在numpy的1.4.1,什么是计算的屏蔽的阵列的直方图的最简单,最有效的方法是什么? numpy.histogram pyplot.hist 不要指望蒙面元素,默认情况下!

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!

推荐答案

(以上取消删除这是每...讨论)

(Undeleting this as per discussion above...)

我不知道该numpy的开发商是否会认为这是一个错误的或预期的行为。我要求在邮件列表,所以我想我们会看到什么,他们说。

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.

无论哪种方式,这是一个容易解决。修补 numpy的/ lib目录/ function_base.py 使用 numpy.asanyarray ,而不是 numpy.asarray 在函数的输入将允许它正确使用蒙面阵列(或ndarray的任何其他子类),而无需创建一个副本。

Either way, it's an easy fix. Patching numpy/lib/function_base.py to use numpy.asanyarray rather than numpy.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.

编辑:看起来这是预期的行为。 由于这里讨论:

It seems like it is expected behavior. As discussed here:

如果你想忽略屏蔽数据是
  只是额外的函数调用

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

直方图(m_arr.com pressed())

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.

使用asanyarray还将使
  在矩阵和其他亚型
  可能不被正确地处理
  直方图计算。

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.

这篇关于如何创建蒙面值的数组的直方图,在numpy的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆