Masked arrays are NumPy arrays that may have missing or invalid entries. The `numpy.ma` module provides a nearly work-alike replacement for NumPy that supports data arrays with masks.
Questions tagged [masked-array]
151 questions
4
votes
2 answers
Get non-masked values in Numpys Masked arrays
I'm trying to extract data from netCDF4 files. These contain "MaskedArrays" which are part of the Numpy library.
My Data contains: latitude, longitude, day and values (separated over different files).
Additionally a mask which shows, which…

neulaender
- 173
- 1
- 3
- 10
4
votes
1 answer
unexpected behaviour of numpy.median on masked arrays
I've a question relating the behaviour of numpy.median() on masked arrays created with numpy.ma.masked_array().
As I've understood from debugging my own code, numpy.median() does not work as expected on masked arrays (see Using numpy.median on a…

Joris
- 168
- 9
4
votes
1 answer
Numpy masked_array sum
I would expect the result of a summation for a fully masked array to be zero, but instead "masked" is returned. How can I get the function to return zero?
>>> a = np.asarray([1, 2, 3, 4])
>>> b = np.ma.masked_array(a, mask=~(a > 2))
>>>…

orange
- 7,755
- 14
- 75
- 139
4
votes
1 answer
How do I keep the mask when slicing a masked array in Numpy?
When I create a view of a Numpy masked array (via slicing) the mask is copied to the view -- so that updates to the view will not change the mask in the original (but will change the data in the original array).
What I want is to change both the…

qff
- 5,524
- 3
- 37
- 62
3
votes
2 answers
in numpy, what is the difference between calling MA.masked_where and MA.masked_array?
Calling masked_array (the class constructor) and the masked_where function both seem to do exactly the same thing, in terms of being able to construct a numpy masked array given the data and mask values. When would you use one or the other?
>>>…

alani
- 12,573
- 2
- 13
- 23
3
votes
1 answer
What is the difference between hard and soft masked numpy arrays?
I have been browsing the numpy docs for masked arrays (which I really like and use regularly) and found numpy.ma.harden_mask and numpy.ma.soften_mask which affect the MaskedArray.hardmask attribute, but I can't find an explanation of that…

hans_meine
- 1,881
- 1
- 17
- 29
3
votes
0 answers
How to retrieve conflicting location in shapely TopologicalError?
When trying to plot a 2D array in cartopy projected axes using contourf, I am receiving a TopologicalError. This is the code:
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import numpy as np
from shapely.geos import…

DRod07
- 33
- 2
3
votes
1 answer
Odd behavior of using += with numpy.array and numpy.ma.array
Can anyone explain the following result to me?
I know it is not as one would usually do this operation, but I found this result odd.
import numpy as np
a = np.ma.masked_where(np.arange(20)>10,np.arange(20))
b =…

pythonewbie
- 33
- 4
3
votes
1 answer
masked RGB image does not appear masked with imshow
I noticed that displaying a RGB masked image does not work as I would expect, i.e. the resulting image is not masked when displayed. Is it normal, is there a workaround?
The example bellow shows the observed behaviour:
import numpy as np
from…

BayesianMonk
- 540
- 7
- 23
3
votes
0 answers
numpy set_printoptions doesnt work for numpy.ma arrays
I am working with masked numpy arrays and trying to print them out in a nice way for debugging purposes. I am setting the print options as below, but the output is not what I expect.
import numpy as np
np.set_printoptions(formatter={'float_kind':…

BenedictWilkins
- 1,173
- 8
- 25
3
votes
2 answers
Merge 2D list of masked arrays with different lengths
I have a very large list of masked arrays, which I want to combine together, but the arrays have different lengths.
To keep it simple this is what I want to do, I want to obtain C:
A=[[--,--,--,...,--]
[1,2,3,...,--,--],
…

Jellyse
- 839
- 7
- 22
3
votes
3 answers
Numpy broadcast_to for masked array
I'm using the np.broadcast_to function to get a view on a reshaped array just like the example:
>>> x = np.array([1, 2, 3])
>>> np.broadcast_to(x, (3, 3))
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
Passing a masked array to this…

Duncan WP
- 830
- 5
- 19
3
votes
1 answer
Efficient memory usage with numpy masked arrays
I have a large ndarray X (roughly (1e3, 1e3, 1e3)), where I want to do manipulations of X including and not including particular elements of the 0th axis (for each element of the 1st and 2nd axes). i.e. there are (1e3, 1e3) elements which I want to…

DilithiumMatrix
- 17,795
- 22
- 77
- 119
3
votes
1 answer
K-means color clustering - omit background pixels with masked numpy arrays
I'm trying to find the 3 dominant colours of an several images using K-means clustering. The problem I'm facing is that K-means also clusters the background of the image. I am using Python 2.7 and OpenCV 3
All images have the same grey background of…

Poehe
- 33
- 1
- 3
3
votes
1 answer
Inverting the "numpy.ma.compressed" operation
I want to create an array from a compressed masked array and a corresponding mask. Its easier to explain this with an example:
>>> x=np.ma.array(np.arange(4).reshape((2,2)), mask = [[True,True],[False,False]])
>>> y=x.compressed()
>>> y
array([ 2, …

MuellerSeb
- 796
- 6
- 11