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
3
votes
1 answer

What practical impact (if any) does the `fill_value` of a masked array have?

When displaying a MaskedArray, I'm told the data, the mask, and the fill value. Of course, data and mask are very important. But what is the practical significance of the fill value? I can even change it, but why would I want to do that — isn't…
gerrit
  • 24,025
  • 17
  • 97
  • 170
2
votes
1 answer

Express 1d masked array as list of slices

I have a 1d numpy array with booleans values (mask) which I would like to convert into a list of slices where the mask is True, e.g.: mask = [False, True, True, True, False, False, True, True] and I would like to obtain [slice(1, 4, None), slice(6,…
jzerfowski
  • 55
  • 6
2
votes
0 answers

Unexpected masked element in numpy's isin() with masked arrays. Bug?

Using numpy and the following masked arrays import numpy.ma as ma a = ma.MaskedArray([[1,2,3],[4,5,6]], [[True,False,False],[False,False,False]]) ta = ma.array([1,4,5]) >>> a masked_array( data=[[--, 2, 3], [4, 5, 6]], mask=[[ True,…
sebschub
  • 213
  • 4
  • 20
2
votes
2 answers

In a one dimensional numpy array of 1's and 0's, how can I convert the next n elements following a 1 to 0's?

For a one dimensional numpy array of 1's and 0's, how can I effectively "mask" the array such that after the occurrence of a 1, the next n elements of the array are converted to zero. After the n elements have passed, the pattern repeats such that…
2
votes
2 answers

Operation on specific array position based on boolean map, boolean mask

I want to have the code to do certain operations on the specified elements of the array based on a boolean map. Let's say I have a numpy array. a = np.array([[-2.5,-3.2],[2.3,5.3],[1.9,-2.8]]) I want to do certain operations, such as np.ceil() for…
Chingyuen
  • 21
  • 3
2
votes
0 answers

Need method to replicate "argpartition" for masked arrays in numpy

As the title indicates I want to use numpy.argpartition on a masked array. I can, but .argpartition does not honor the mask (it emits a warning message notifying the user as well). This is not useful since the masked data corrupt the results of…
Jim Parker
  • 1,095
  • 11
  • 16
2
votes
0 answers

Check a row for ascension in Numpy, but ignoring elements = 0

I have the code snippet below that searches each row/column in an array to see if all values are either ascending or descending. Ideally, this code would ignore zeros. For example, a row with (5, 0, 3, 1) would come up True for descending. The…
2
votes
3 answers

Comparing two numpy arrays for compliance with two conditions

Consider two numpy arrays having the same shape, A and B, composed of 1s and 0s. A small example is shown: A = [[1 0 0 1] B = [[0 0 0 0] [0 0 1 0] [0 0 0 0] [0 0 0 0] [1 1 0 0] [0 0 0 0] …
user109387
  • 600
  • 3
  • 11
2
votes
1 answer

difference between netcdf4 _FillValue, missing_value, and python masked array fill_value

I have netcdf files where _FillValue and/or missing value are set in various combinations (e.g. as float, string, or not at all, and generally as -999./'-999' ). I am manipulating these files and saving them again and want to set the fill value…
Therese
  • 63
  • 1
  • 2
  • 6
2
votes
2 answers

Trace back original position of argmin/argmax on boolean masked NumPy array - Python

Context Since masking with the numpy.ma-module is significantly slower than direct boolean masking, I have to use the latter for my argmin/argmax-calculations. A little comparison: import numpy as np # Masked Array arr1 =…
Markus
  • 2,265
  • 5
  • 28
  • 54
2
votes
1 answer

Creating numpy masked array on 3 channel array never works

I have a 3 channel numpy array, ie an image and I want to mask out some areas then calculate the mean on the unmasked areas. When I go to convert my numpy array to a masked numpy array I always get the following error: raise MaskError(msg % (nd,…
sazr
  • 24,984
  • 66
  • 194
  • 362
2
votes
1 answer

Masking only a certain range of indices in numpy

I have an ndarray. I need to mask any number less than 50 until the first number encountered is greater than 50. This should be done both at the beginning and at the end of a row. Right, when the first number encountered is >50, the masking should…
a_r
  • 488
  • 6
  • 12
2
votes
1 answer

Using logical_and to combine numpy masks

I've seen similar questions asked but I'm still struggling with combining masks. This is my code: final_mask = ma.array(np.logical_and(a.mask, b.mask)) combined_mask=(ma.array(data, mask=final_mask)) data is the array I want to put the combined…
JaneOz
  • 21
  • 1
  • 4
2
votes
1 answer

Generate mask array with lowest N valued positions reset per row

Given a 2D array of distances, use argsort to generate an index array, where the first element is the index of the lowest value in the row. Use indexing to select only the first K columns, where K = 3 for example. position = np.random.randint(100,…
2
votes
1 answer

Operations on numpy masked array gives invalid values masked

From the documentation on masked arrays in numpy operations on numpy arrays: The numpy.ma module comes with a specific implementation of most ufuncs. Unary and binary functions that have a validity domain (such as log or divide) return the masked…
salotz
  • 429
  • 4
  • 20
1 2
3
10 11