# 掩码数组

# 什么是掩码数组？

>>> import numpy as np
>>> import numpy.ma as ma
>>> x = np.array([1, 2, 3, -1, 5])


>>> mx = ma.masked_array(x, mask=[0, 0, 0, 1, 0])


>>> mx.mean()
2.75


#numpy.ma模块

numpy.ma 模块的主要特性是MaskedArrayopen in new window 类，它是的子类numpy.ndarrayopen in new window。 在MaskedArray类open in new window部分中更详细地描述了类、其属性和方法。

numpy.ma 模块可以用作 numpy 的补充：

>>> import numpy as np
>>> import numpy.ma as ma


>>> y = ma.array([1, 2, 3], mask = [0, 1, 0])


>>> z = masked_values([1.0, 1.e20, 3.0, 4.0], 1.e20)


# 使用 numpy.ma 模块

# 仅访问有效条目

>>> x = ma.array([[1, 2], [3, 4]], mask=[[0, 1], [1, 0]])
fill_value = 999999)


>>> x.compressed()
array([1, 4])


# 修改掩码

# 掩码条目

>>> x = ma.array([1, 2, 3])
>>> x
mask = [ True False False],
fill_value = 999999)
>>> y = ma.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> y[(0, 1, 2), (1, 2, 0)] = ma.masked
>>> y
[[1 -- 3]
[4 5 --]
[-- 8 9]],
[[False  True False]
[False False  True]
[ True False False]],
fill_value = 999999)
>>> z = ma.array([1, 2, 3, 4])
>>> z
masked_array(data = [-- -- 3 4],
mask = [ True  True False False],
fill_value = 999999)


>>> x = ma.array([1, 2, 3], mask=[0, 0, 1])
>>> x
mask = [ True  True  True],
fill_value = 999999)


>>> x = ma.array([1, 2, 3])
>>> x.mask = [0, 1, 0]
>>> x
fill_value = 999999)


# 取消掩码条目

>>> x = ma.array([1, 2, 3], mask=[0, 0, 1])
>>> x
fill_value = 999999)
>>> x[-1] = 5
>>> x
fill_value = 999999)


>>> x = ma.array([1, 2, 3], mask=[0, 0, 1], hard_mask=True)
>>> x
fill_value = 999999)
>>> x[-1] = 5
>>> x
fill_value = 999999)
>>> x[-1] = 5
>>> x
fill_value = 999999)


>>> x = ma.array([1, 2, 3], mask=[0, 0, 1])
>>> x
fill_value = 999999)
>>> x
fill_value = 999999)


# 索引和切片

>>> x = ma.array([1, 2, 3], mask=[0, 0, 1])
>>> x[0]
1
>>> x[-1]
fill_value = 1e+20)
True


>>> y = ma.masked_array([(1,2), (3, 4)],
...               dtype=[('a', int), ('b', int)])
>>> y[0]
(1, 2)
>>> y[-1]
fill_value = (999999, 999999),
dtype = [('a', '<i4'), ('b', '<i4')])


>>> x = ma.array([1, 2, 3, 4, 5], mask=[0, 1, 0, 0, 1])
>>> mx = x[:3]
>>> mx
fill_value = 999999)
>>> mx[1] = -1
>>> mx
fill_value = 999999)
array([False,  True, False, False,  True])
>>> x.data
array([ 1, -1,  3,  4,  5])


# 掩码数组的操作

numpy.ma模块附带了大多数ufunc的特定实现。 只要输入被掩码或超出有效域，具有有效域（例如logopen in new windowdivideopen in new window）的一元和二元函数 maskedopen in new window就会返回常量：

>>> ma.log([-1, 0, 1, 2])
masked_array(data = [-- -- 0.0 0.69314718056],
mask = [ True  True False False],
fill_value = 1e+20)


>>> x = ma.array([-1, 1, 0, 2, 3], mask=[0, 0, 0, 0, 1])
>>> np.log(x)
masked_array(data = [-- -- 0.0 0.69314718056 --],
mask = [ True  True False False  True],
fill_value = 1e+20)


# 示例

# 具有表示缺失数据的给定值的数据

>>> import numpy.ma as ma
>>> x = [0.,1.,-9999.,3.,4.]
>>> mx = ma.masked_values (x, -9999.)
>>> print mx.mean()
2.0
>>> print mx - mx.mean()
[-2.0 -1.0 -- 1.0 2.0]
>>> print mx.anom()
[-2.0 -1.0 -- 1.0 2.0]


# 填写缺失的数据

>>> print mx.filled(mx.mean())
[ 0.  1.  2.  3.  4.]


# 数值运算

>>> import numpy as np, numpy.ma as ma
>>> x = ma.array([1., -1., 3., 4., 5., 6.], mask=[0,0,0,0,1,0])
>>> y = ma.array([1., 2., 0., 4., 5., 6.], mask=[0,0,0,0,0,1])
>>> print np.sqrt(x/y)
[1.0 -- -- 1.0 -- --]


# 忽略极值

>>> print ma.masked_outside(d, 0.1, 0.9).mean()