Apply Mask Array 2d to 3d

2024/10/7 18:30:10

I want to apply a mask of 2 dimensions (an NxM array) to a 3 dimensional array (a KxNxM array). How can I do this?

2d = lat x lon

3d = time x lat x lon

import numpy as npa = np.array([[[ 0,  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]]])b = np.array([[ 0, 1, 0],[ 1, 0, 1],[ 0, 1, 1]])c = np.ma.array(a, mask=b)  # this behavior is wanted 
Answer

There are quite a few different ways to choose from. What you want to do is align the mask (of lower dimension) to the array that has the extra dimension: the important part is that you get the number of elements in both arrays the same, as the first example shows:

np.ma.array(a, mask=np.concatenate((b,b,b)))  # shapes are (3, 3, 3) and (9, 3)
np.ma.array(a, mask=np.tile(b, (a.shape[0],1)))  # same as above, just more general as it doesn't require you to specify just how many times you need to stack b.
np.ma.array(a, mask=a*b[np.newaxis,:,:])  # used broadcasting
https://en.xdnf.cn/q/70208.html

Related Q&A

Server Side Google Markers Clustering - Python/Django

After experimenting with client side approach to clustering large numbers of Google markers I decided that it wont be possible for my project (social network with 28,000+ users).Are there any examples …

Tensorflow numpy image reshape [grayscale images]

I am trying to execute the Tensorflow "object_detection_tutorial.py" in jupyter notebook, with my trained neural network data but it throws a ValueError. The file mentioned above is part of S…

Merge numpy arrays returned from loop

I have a loop that generates numpy arrays:for x in range(0, 1000):myArray = myFunction(x)The returned array is always one dimensional. I want to combine all the arrays into one array (also one dimensio…

Play mp3 using Python, PyQt, and Phonon

I been trying all day to figure out the Qts Phonon library with Python. My long term goal is to see if I could get it to play a mms:// stream, but since I cant find an implementation of this done anywh…

Python dictionary keys(which are class objects) comparison with multiple comparer

I am using custom objects as keys in python dictionary. These objects has some default hash and eq methods defined which are being used in default comparison But in some function i need to use a diffe…

How can I make np.save work for an ndarray subclass?

I want to be able to save my array subclass to a npy file, and recover the result later.Something like:>>> class MyArray(np.ndarray): pass >>> data = MyArray(np.arange(10)) >>&g…

With ResNet50 the validation accuracy and loss is not changing

I am trying to do image recognition with ResNet50 in Python (keras). I tried to do the same task with VGG16, and I got some results like these (which seem okay to me): resultsVGG16 . The training and v…

string has incorrect type (expected str, got spacy.tokens.doc.Doc)

I have a dataframe:train_review = train[review] train_reviewIt looks like:0 With all this stuff going down at the moment w... 1 \The Classic War of the Worlds\" by Timothy Hi... 2 T…

custom URLs using django rest framework

I am trying to use the django rest framework to expose my models as APIs.serializersclass UserSerializer(serializers.HyperlinkedModelSerializer):class Meta:model = Userviewsetclass UserViewSet(viewsets…

Does python logging.FileHandler use block buffering by default?

The logging handler classes have a flush() method. And looking at the code, logging.FileHandler does not pass a specific buffering mode when calling open(). Therefore when you write to a log file, it …