Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Changing element in 2D numpy array to nan

sample = np.array([[-1, 1, -1, 1], [-1, 2, -1, 2], [-1, 3, 3, 3] ,[-1, 4, 4, 4], [-1, 5, 5, 5], [6, 6, 6, -1], [7, 7, 7, -1], [8, 8, -1, -1], [9, 9, -1, -1]])
float_sample = sample.astype(np.float64)

for row in float_sample:
    for ele in row:
        if (ele == -1.):
            float_sample[row][ele] = np.nan

I am trying to iterate through a 2D numpy array and whenever I see -1, convert it to NaN, np.nan. But whenever I try to do it doing the iteration I have above I get the following error message:

"Arrays used as indices must be of integer (or boolean) type"

How to I fix it so that I can iterate through a 2D numpy array of type float and whenever it finds a -1, convert it to NaN? I am doing this so that I can take the median of each column but you can’t do that using masked arrays so I am stuck trying to do it with a normal numpy array.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use numpy’s boolean indexing:

import numpy as np

sample = np.array(
    [[-1, 1, -1, 1], [-1, 2, -1, 2], [-1, 3, 3, 3], [-1, 4, 4, 4], [-1, 5, 5, 5], [6, 6, 6, -1], [7, 7, 7, -1],
     [8, 8, -1, -1], [9, 9, -1, -1]])

float_sample = sample.astype(np.float64)
float_sample[sample == -1] = np.nan
print(float_sample)

Output

[[nan  1. nan  1.]
 [nan  2. nan  2.]
 [nan  3.  3.  3.]
 [nan  4.  4.  4.]
 [nan  5.  5.  5.]
 [ 6.  6.  6. nan]
 [ 7.  7.  7. nan]
 [ 8.  8. nan nan]
 [ 9.  9. nan nan]]

As a side note the problem is that you are using the row to index in:

float_sample[row][ele] = np.nan

and row is an array of floats.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading