Finding missing values ​​in a numpy array

Alright, extreme rookie question. In my program, I create a numpy 2D array, some of which are missing entries (rather than a "nan" type of nonexistent, but a "None", or NoneType). I would like to mask these entries, but it looks like I'm having problems with this. Normally, to mask, say, all records with a value of 2, I would do

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

In this case, this does not work no matter what I try to use for the first parameter. Thoughts?

+5
source share
2 answers

Since you have entries --in your array, I assume this means they are already masked:

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

, , .

, ,

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

m - .

, :

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

, None, , . None.

, :

>>> numpy.nonzero(m.mask)

documentation numpy.nonzero() , .

+5

numpy, None, numpy.equal. :

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 ,-- ,]
+5
source

All Articles