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
0
votes
2 answers
Is there a way to disregard masked values in an array used to mask separate array?
My data is several arrays of data taken of the same length. I am masking one array (y) then using that masked array to mask a 2nd array (x). I mask x to get rid of values indicating equipment error (-9999). I then use np.where() to find out where y…

danrod13
- 91
- 1
- 8
0
votes
0 answers
Avoid masked array elements being formatted as '0.00'
I have a masked array in numpy:
masked = np.ma.core.MaskedArray([1, 2], mask=[True, False])
Now I want to display the numbers in a plot, so I need formatting:
"{:.2f}".format(masked[0])
'0.00'
"{:.2f}".format(masked[1])
'2.00'
Why is a masked…

user3017048
- 2,711
- 3
- 22
- 32
0
votes
0 answers
Interpolate in 1D a 3D masked array in python
I need a faster solution to interpolate a 3D array in one dimension to a 2D surface. My data is gridded (k, j, i): the horizontal grid is regular (j, i), but the vertical grid is not (i.e depth points are different at each i, j point). Additionally,…

Isa
- 1
- 3
0
votes
1 answer
Shift masked values of to the end of a 2D python array?
Suppose that we have a 2D numpy array and we use np.ma.masked_where to mask some elements. In each row, we want to move all the masked elements to the end while preserving the order of rest of the elements. Is there an easy loop-free way to do…

Joe
- 79
- 3
0
votes
2 answers
Demean and standardise a 4d array within a 3d mask?
import numpy as np
ts = np.random.rand(40,45,40,1000)
mask = np.random.randint(2, size=(40,45,40),dtype=bool)
#creating a masked array
ts_m = np.ma.array(ts, mask=ts*~mask[:,:,:,np.newaxis])
#demeaning
ts_md = ts_m -…

themachinist
- 1,413
- 2
- 17
- 22
0
votes
1 answer
scipy rankdata with masked array
I notice the following strange behavior with rankdata with maksed_array. Here is the code:
import numpy as np
import scipy.stats as stats
m = [True, False]
print(stats.mstats.rankdata(np.ma.masked_array([1.0, 100], mask=m)))
# result [0.…

fivelements
- 1,487
- 2
- 19
- 32
0
votes
3 answers
Masking a 2D array and operating on second array based off masked indices
I have a function that reads in and outputs a 2D array. I want the output to be constant (pi in this case) for every index in the input that equals 0, otherwise I perform some maths on it. E.g:
import numpy as np
import numpy.ma as ma
def…

rh1990
- 880
- 7
- 17
- 32
0
votes
1 answer
Delete masked elements from a list of arrays
I'm trying to plot a folium heatmap in Jupyter, but I get the error ValueError: Location values cannot contain NaNs, got:
[nan, nan]
So I need to delete the masked elements from a list of arrays. My arrays look like this:
Xpos
Out[68]:…

Jellyse
- 839
- 7
- 22
0
votes
3 answers
Append list array in for loop
I have a masked array Xpos:
masked_array(
data=[[--, --, --, --, --, --, --, --, --, --],
[--, --, --, --, --, --, --, --, --, --],
[--, --, --, --, --, --, --, --, --, --],
[--, --, --, --, --, --, --, --, --, --],
[--, --, --,…

Jellyse
- 839
- 7
- 22
0
votes
1 answer
Counting masked values from masked arrays
I have a numpy masked array called 'mask' and I was wondering how I would be able to count the amount of either True or False values in the mask?
The mask variable is created when looping through different datasets so it would be helpful if it would…

Max Collier
- 573
- 8
- 24
0
votes
2 answers
Numpy - even more position-based array modification
I have a large 2-dimensional numpy array, with every value being a 0 or a 1. I'd like to create a function which takes this array as an input, and returns a new array of the same size in which each element is based on the elements above, below and…

Preamble
- 3
- 1
0
votes
1 answer
Python masked arrays producing NaNs with np.array
I am trying to select a subset of a larger grid to perform a finite element analysis to iterate over between two arrays; a masked array and an unmasked array but i have come across a problem of NaNs appearing from the mask when using np.arrays. I…

Sir Hat
- 18
- 3
0
votes
1 answer
numpy create nxn array of nx2 dot product of nxnx2 array
My aim is to create an nxn array of the angle between one particle and every other particle in a simulation, for all particles. Then a masked array can select all particles in a particular field of view.
My question is how to take two nxn arrays…

Blue Shrapnel
- 95
- 1
- 13
0
votes
0 answers
How do I create a 2d image array out of 3 masked arrays?
So I'm trying to create an image out of the mean pixel values from images that have been saved as 2d arrays. With the dark frames this was fairly easy, I just had to create a running average. With exposure time it's a bit more complicated because I…

Spider Jerusalem
- 61
- 9
0
votes
1 answer
Mask ndarray with boolean ndarray to replace nans
I'm trying to use a boolean mask to address rows in a numpy array:
isnan = np.isnan(self.X[:, AGE_COLUMN].astype(float))
self.X[isnan, AGE_COLUMN] = np.mean(self.X[:, AGE_COLUMN].astype(float))
isnan and X are dtype.
First I check which rows in the…

L3n95
- 1,505
- 3
- 25
- 49