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
1
vote
0 answers
Cropping an image using a mask
I have an image which has a pixel array of [7981,7891]. I have assigned each pixel a lat lon coordinate via interpolation. Now I want to try and cut out a square section of the image so that the rest of my code only works on this small section. Here…

JC1217
- 79
- 1
- 3
1
vote
1 answer
How to create masked array with a vector that separates sections of a 2d array?
Let's say I have a standard 2d numpy array, let's call it my2darray with values. In this array there are two major sections. Let's say for each column, there is a specific row which separates "scenario1" and "scenario2". How can i create 2 masked…

user2015487
- 2,075
- 3
- 17
- 20
1
vote
1 answer
What am I doing wrong with numpy searchsorted?
It's kind a funny behavior in numpy.searchsorted. The following test fail:
import numpy as np
a = np.ma.masked_array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,…

jmbarrios
- 110
- 7
1
vote
1 answer
RBF Kernel on Masked array
I wonder if there is a way to compute the Gaussian kernel of a numpy masked array?
I import:
from sklearn.metrics.pairwise import rbf_kernel
If one uses a masked array and gives it as the input to the rbf_kernel function of scikit learn package…

Sinooshka
- 511
- 5
- 10
1
vote
3 answers
Numpy masked array modification
Currently I have a code that checks if given element in array is equal = 0 and if so then set the value to 'level' value (temp_board is 2D numpy array, indices_to_watch contains 2D coordinates that should be watched for zeros).
indices_to_watch…

Krzysztof Krasoń
- 26,515
- 16
- 89
- 115
0
votes
0 answers
Why does np.ma.median work for only certain sizes of nan matrices and give the MaskError('Cannot alter the masked element.') for other sizes
I am trying to build a code that can be given an matrix of nans and not break. I wrote a simplified version here that recreates the problem. I am using numpy version 1.23.5
Basically the code takes a grid of nans and creates a annulus mask to apply…

loberhel
- 1
- 1
0
votes
0 answers
Masked array from list of one-element masked arrays: Cannot convert masked element to a Python int
I want to create a 1d masked array from a list of one-element (0d) masked arrays using numpy.ma. This works as expected if the values are not masked, it does not in the other case:
import numpy.ma as ma
ma.MaskedArray([ma.masked_array(data=-99,…

sebschub
- 213
- 4
- 20
0
votes
1 answer
Adding 3D masked arrays results in TypeError: 'numpy.bool_' object is not iterable
I have two 3D masked arrays (netCDF4 files output from climate model) that I want to add together. I followed this thread and got the following (simplified) code out of it:
import numpy as np
from netCDF4 import Dataset
from operator import…

ElsRi
- 37
- 1
- 5
0
votes
0 answers
correlation calculation of masked array:
I have a sea surface temperature time series, and I want to calculate the correlation coefficients among the nodes in the tropics(30.0N to 30S). In the time series, the land information is present as masked data. I do not know how to handle masked…

Ruby
- 39
- 5
0
votes
0 answers
Why does the scipy.ndimage.gaussian_filter changes the shape of my masked image?
I would like to apply a Gaussian filter to a masked array, however, this appears to alter the shape of my image. The higher I put sigma, the more of the image disappears. I don't really understand why this happens? I would expect both figures to…

Emma
- 3
- 3
0
votes
1 answer
problem with setting numpy's maskedarray fill_value
I cannot figure out how to set the fill_value of a real masked array to be np.nan. The array is the result of the calculation of two complex maskedarrays. Somehow, the calculated array's fill_value always gets converted to a complex fill_value,…

jbiz
- 43
- 5
0
votes
1 answer
How to solve numpy.ma.core.MaskError: Cannot alter the masked element?
I am new to python and I am trying to convert python 2 code I found on github (https://github.com/RDCEP/psims) into python 3. It went quite well, but now I am stuck with the following error message:
Traceback (most recent call last):
File…

albren
- 109
- 1
- 1
- 8
0
votes
1 answer
How to apply a function element-wise with inputs from multiple numpy masked arrays to create a new masked array?
I have a function that takes in 4 single value inputs to return a singular float output, for example:
from scipy.stats import multivariate_normal
grid_step = 0.25 #in units of sigma
grid_x, grid_y = np.mgrid[-2:2+grid_step:grid_step,…

Ho-Hin Leung
- 1
- 1
0
votes
1 answer
Irregular/Inhomogeneous Arrays with JAX
What is the recommended approach to implement array behaviour/methods on irregular/inhomogeneous data (possesses some inherient dimensionality) within JAX?
Two principle options come to mind:
make homogeneous and use a mask
flatten and implement…

DavidJ
- 326
- 2
- 10
0
votes
1 answer
How to implement masked array fitting using lmfit (prior using sigma option of curve_fit)
I am trying to implement masked-array fitting using lmfit.
Prior I was using curve_fit using the following code snippet. I have omitted the def spec due to its line length. Essentially def spec is function that performs 50 interpolations of…

cosmic noise
- 47
- 6