Values ​​Modified in a numpy Array

So, I have a numpy 2D array (256,256) containing values ​​from 0 to 10, which is essentially an image. I need to remove the values ​​0 and set them to NaN so that I can build an array using a specific library (APLpy). However, whenever I try to change all the values ​​of 0, some other values ​​change, in some cases up to 100 times their original value (I don’t know why).

The code I use is:

for index, value in np.ndenumerate(tex_data):
    if value == 0:
        tex_data[index] = 'NaN'

where tex_data is the data array from which I need to remove zeros. Unfortunately, I can't just use a mask for values ​​that I don't need, since APLpy will not use masked arrays, as far as I can tell.

In any case, can I set the values ​​0 to NaN without changing other values ​​in the array?

+4
source share
2 answers

Use fancy-indexing. Like this:

tex_data[tex_data==0] = np.nan

I do not know why your source code failed. It looks right to me, albeit terribly inefficient.

+4
source

Using float rules,

tex_data/tex_data*tex_data

do the work here.

0
source

Source: https://habr.com/ru/post/1667914/


All Articles