numpy:堆叠被遮罩的数组并计算最小/最大 [英] Numpy: Stacking masked Arrays and calculating min/max

查看:87
本文介绍了numpy:堆叠被遮罩的数组并计算最小/最大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用蒙版数组,我想计算不同数组/列的最大值.如果整个数组都被遮罩,我会遇到问题.

I'm working with masked arrays and I want to calculate the max of different arrays/columns. I have problems, if the whole array is masked.

示例:

import numpy as np

x = np.ma.array(np.array([1,2,3,4,100]),mask=[True,True,True, True, True])
y = 5

print(np.max(np.hstack((x, y))))
print np.max((np.max(y), np.max(x)))
print(np.max((np.hstack((np.max(x), 5)))))

结果:

100
nan
--

我发现结果很奇怪,因为结果应该是5.为什么hstack()忽略了 遮罩数组的遮罩?

I find the result odd, because the result should be 5. Why is hstack() ignoring the mask of the masked array?

推荐答案

对于带掩码的数组,您需要使用带掩码的例程,即numpy.ma.应该在方法名称之前:

With masked arrays, you need to use masked routines, that is numpy.ma. should precede method name:

>>> np.ma.hstack((x, y))
masked_array(data = [-- -- -- -- -- 5],
             mask = [ True  True  True  True  True False],
       fill_value = 999999)

>>> np.ma.max(np.ma.hstack((x, y)))
5

这篇关于numpy:堆叠被遮罩的数组并计算最小/最大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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