0

In my real problem, I have hundreds of data arrays and result arrays, so I cannot do it manually.

import numpy as np

data1 = np.array([1,2,np.nan,4,5,6,7],dtype=float)
data2 = np.array([11,np.nan,9,4,5,6,71],dtype=float)
data3 = np.array([17,np.nan,13,4,15,6,17],dtype=float)

my_groups = ['result1', 'result2', 'result3']

result1 = data1/data2
result2 = data1/data3
result3 = data3/data2

For every result, I want to convert the np.nan values to -9.0: The longest way of writing is:

result1 = np.where(np.isnan(result1),-9.0,result1)
result2 = np.where(np.isnan(result2),-9.0,result2)
result3 = np.where(np.isnan(result3),-9.0,result3)

But, I want to shorten the script. What is the good way of doing it?

for i in my_groups:
    out[i] = np.where(np.isnan(i),-9.0,i)

Of course my code is wrong. Waiting for your ideas.

2
  • "I cannot do it manually. I have hundreds of arrays" So define a function which takes an array as an argument. Commented Dec 10, 2018 at 0:32
  • If all your arrays have shape 1x7 and there are N of them, why not just declare one big Nx7 array? Then you can do all your processing at once. (Or if you know their max length is M, can you declare one NxM array?) You haven't given enough specifics on your task. Commented Dec 10, 2018 at 2:31

1 Answer 1

2

Simply use a mask on any array X:

X[np.isnan(X)] = -9.0

This is exactly what numpy does in a similar function nan_to_num which replaces nan with zero and inf with finite numbers. If your "data" arrays are all of the same size, you could accomplish this in one fell swoop by simply using a 2D array. For example:

import numpy as np

data = np.array([[1,2,np.nan,4,5,6,7],
                 [11,np.nan,9,4,5,6,71],
                 [17,np.nan,13,4,15,6,17]])

result = np.array([data[0]/data[1],
                   data[0]/data[2],
                   data[2]/data[1]])

result[np.isnan(result)] = -9
print result

>> [[ 0.09090909 -9.         -9.          1.          1.          1.       0.09859155]
    [ 0.05882353 -9.         -9.          1.          0.33333333  1.       0.41176471]
    [ 1.54545455 -9.          1.44444444  1.          3.          1.       0.23943662]]
Sign up to request clarification or add additional context in comments.

6 Comments

yes this is a long way of doing it. i have to do it separately for each array. i want to shorten it
But how to put array of three results into X?
@neha I'ved added an example that hopefully clears things up.
But in my real problem, i have hundreds of data arrays and hundreds of results array. So, I can not not add them into 2d array manually
@neha That would have been good information to know in the question. Where does the data come from, simulation, from a file? You can always throw np.array around a list of lists.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.