开发者

Finding missing values in a numpy array

Alright, extreme rookie question here. In my program, I generate a 2D numpy array, some of whom's entries are missing (not the "nan" kind of nonexistant, but the "None" kind, or NoneType). I'd like to put a mask over these entries, 开发者_StackOverflow社区but I seem to be having some trouble doing so. Ordinarily, to mask over, say, all entries with value 2, I'd do

A = np.ma.masked_where(A[A==2], A)

In this case, that doesn't seem to work no matter what I try for the first parameter. Thoughts?


Since you have -- entries in your array, I guess that it means that they are already masked:

>>> m = ma.masked_where([True, False]*5, arange(10))
>>> print m
[-- 1 -- 3 -- 5 -- 7 -- 9]

So, I would say that your entries are already masked and that you can directly use your array.

If you want to create an array that only contains the non-masked value, you can do

>>> m[~m.mask]
[1 3 5 7]

where m is your masked array.

If you want to have the list of masked values, you can simply select the other values:

>>> m[m.mask]
[0 2 4 6 8]

Note that the missing values are not None, but are the original values, generally. In fact, an array of integers cannot contain None.

If you want the indices of the masked values, you can do:

>>> numpy.nonzero(m.mask)

The documentation of numpy.nonzero() describes how its result must be interpreted.


To find the elements in a numpy array that are None, you can use numpy.equal. Here's an example:

import numpy as np
import MA

x = np.array([1, 2, None])

print np.equal(x, None)
# array([False, False,  True], dtype=bool)

# to get a masked array
print MA.array(x, mask=np.equal(x,None))
# [1 ,2 ,-- ,]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜