擅长:python、mysql、java
<p>在纽比有一个特别的面具班,正是这样做的。可以基于任何前提条件“屏蔽”元素。这比指定零更好地表示您的需要:适当时,numpy操作将忽略屏蔽值(例如,查找平均值)。</p>
<pre><code>>>> from numpy import ma
>>> x = ma.array([.06, .25, 0, .15, .5, 0, 0, 0.04, 0, 0])
>>> x1 = ma.masked_inside(0, 0.1) # mask everything in 0..0.1 range
>>> x1
masked_array(data = [-- 0.25 -- 0.15 0.5 -- -- -- -- --],
mask = [ True False True False False True True True True True],
fill_value = 1e+20)
>>> print x.filled(0) # Fill with zeroes
[ 0 0.25 0 0.15 0.5 0 0 0 0 0 ]
</code></pre>
<p>作为一个附加的好处,如果需要,在matplotlib可视化库中可以很好地支持屏蔽数组。</p>
<p><a href="http://docs.scipy.org/doc/numpy/reference/maskedarray.html#maskedarray" rel="noreferrer">Docs on masked arrays in numpy</a></p>