Questions tagged [masked-array]

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.

151 questions
40
votes
7 answers

How to properly mask a numpy 2D array?

Say I have a two dimensional array of coordinates that looks something like x = array([[1,2],[2,3],[3,4]]) Previously in my work so far, I generated a mask that ends up looking something like mask = [False,False,True] When I try to use this mask on…
pretzlstyle
  • 2,774
  • 5
  • 23
  • 40
13
votes
2 answers

Why are Numpy masked arrays useful?

I've been reading through the masked array documentation and I'm confused - what is different about MaskedArray than just maintaining an array of values and a boolean mask? Can someone give me an example where MaskedArrays are way more convenient,…
RedPanda
  • 522
  • 6
  • 15
11
votes
1 answer

How to mask numpy structured array on multiple columns?

I have a numpy structured array with a dtype such as: A = numpy.empty(10, dtype=([('segment', '
jlconlin
  • 14,206
  • 22
  • 72
  • 105
8
votes
1 answer

xarray with masked arrays while preserving integer dtypes

Currently, my code heavily uses structured masked arrays with multidimensional dtypes, with dozens of fields and item sizes of many kilobytes. It appears that xarray could be a great alternative, but when I try to pass it a masked array, it changes…
gerrit
  • 24,025
  • 17
  • 97
  • 170
7
votes
3 answers

Create mask from skimage contour

I have an image that I found contours on with skimage.measure.find_contours() but now I want to create a mask for the pixels fully outside the largest closed contour. Any idea how to do this? Modifying the example in the documentation: import…
Joe Flip
  • 1,076
  • 4
  • 21
  • 37
6
votes
1 answer

How can I mask elements of a record array in Numpy?

I understand how to create a masked array, and I would like to use masking in a record array so that I can access this data using named attributes. The masking seems to be "lost" when I create a record array from a masked array: >>> data =…
Nate Reed
  • 6,761
  • 12
  • 53
  • 67
6
votes
2 answers

replace masked with nan in numpy masked_array

>> masks = [[1,1],[0,0]] >> [np.ma.masked_array(data=np.array([1.0,2.0]), mask=m, fill_value=np.nan).mean() for m in masks] [masked, 1.5] I'd like to replace the masked result with nan. Is there a way to do that directly with numpy's…
HappyPy
  • 9,839
  • 13
  • 46
  • 68
6
votes
2 answers

Return masked array as simple array with masked values as None

I need to mask an array a by a condition fulfilled by another array b. For example values in a should only be preserved if the values in the same position of b equal 0, otherwise return as None. For example: a = np.array([2, 2, 4, 0, 4, 3, 3, 3, 1,…
MoMiJi
  • 93
  • 1
  • 1
  • 9
6
votes
3 answers

How can I change the value of a masked array in numpy?

In my code, at some point I try to modify a value of a masked array, yet python seems to ignore this. I'm thinking this has to do with the way memory is stored in arrays, as if I were modifying a copy of the value and not the value itself, but I'm…
Jesse Rio
  • 87
  • 1
  • 1
  • 8
6
votes
6 answers

Calculate moving average in numpy array with NaNs

I am trying to calculate the moving average in a large numpy array that contains NaNs. Currently I am using: import numpy as np def moving_average(a,n=5): ret = np.cumsum(a,dtype=float) ret[n:] = ret[n:]-ret[:-n] return…
krakenwagon
  • 113
  • 1
  • 4
5
votes
1 answer

Plotting segmented color images using numpy masked array and imshow

I'm new to numpy's masked array data-structure, and I want to use it to work with segmented color images. When I use matplotlib's plt.imshow( masked_gray_image, "gray") to display a masked gray image, the invalid regions will be displayed…
Max
  • 485
  • 4
  • 13
5
votes
2 answers

np.where and masked array

I'm working with masked arrays thanks to some of the help I've gotten on stackoverflow, but I'm running into a problem with the np.where evaluation of a masked array. My masked array is: m_pt0 = np.ma.masked_array([1, 2, 3, 0, 4, 7, 6, 5], …
stagermane
  • 1,003
  • 2
  • 12
  • 29
5
votes
1 answer

numpy.ma (masked) array mean method has inconsitent return type

I noticed that the numpy masked-array mean method returns different types when it probably should not: import numpy as np A = np.ma.masked_equal([1,1,0], value=0) B = np.ma.masked_equal([1,1,1], value=0) # no masked…
dermen
  • 5,252
  • 4
  • 23
  • 34
4
votes
3 answers

How to get single median in numpy masked array with even number of entires

I have a numpy masked nd-array. I need to find the median along a specific axis. For some cases, I end up having even number of elements, in which case numpy.ma.median gives average of the middle two elements. However, I don't want the average. I…
Nagabhushan S N
  • 6,407
  • 8
  • 44
  • 87
4
votes
5 answers

Filter elements from list based on True/False from another list

Is there an idiomatic way to mask elements of an array in vanilla Python 3? For example: a = [True, False, True, False] b = [2, 3, 5, 7] b[a] I was hoping b[a] would return [2, 5], but I get an error: TypeError: list indices must be integers or…
at.
  • 50,922
  • 104
  • 292
  • 461
1
2 3
10 11