2

Here is the scenario:

I have the following variables:

val = [('am', '<f8'), ('fr', '<f8')] # val is type numpy.recarray     

am = [12.33, 1.22, 5.43, 15.23]  # am is type numpy.ndarray  

fr = [0.11, 1.23, 2.01, 1.01]   # fr is type numpy.ndarray  

What I need is to detect the index for am = 12.33 and am = 15.23, once extracted (in this case indexes are [0] and [3]), I need to create the new variables:

new_am = [12.33, 15.23] 

new_fr = [0.11, 1.01] 

My question is: Any idea about how to extract the indexes?

I have already used .index and np.where but it seems to have problems since I received an error message for .index:

"AttributeError: 'numpy.ndarray' object has no attribute ".index" 

and for np.where the returning index is nothing array([], dtype=int64)

Thanks for any idea!

4
  • So you want the index as np.array([0, 3]) here? np.where(am == 12.33)[0] doesn't give you what you need? Commented Dec 28, 2016 at 2:06
  • Correct, and with that...produce the "new_am" and "new_fr" represented above. Commented Dec 28, 2016 at 2:08
  • no...np.where(am == 12.33)[0] return an empty array Commented Dec 28, 2016 at 2:11
  • Make sure am and fr are numpy array before hand. Commented Dec 28, 2016 at 2:12

1 Answer 1

3

You may want the np.in1d which returns an array of boolean to indicate if an element is in another array:

import numpy as np
am = np.array([12.33, 1.22, 5.43, 15.23]) 
fr = np.array([0.11, 1.23, 2.01, 1.01])

index = np.where(np.in1d(am, [12.33, 15.23]))[0]
index
# array([0, 3])

am[index]
# array([ 12.33,  15.23])

fr[index]
# array([ 0.11,  1.01])

Or maybe you have an array with attributes:

new_arr = np.array(zip(am, fr), dtype=val)

index = np.where(np.in1d(new_arr['am'], [12.33, 15.23]))[0]

new_am = new_arr[index]['am']
new_fr = new_arr[index]['fr']

new_am
# array([ 12.33,  15.23])

new_fr
# array([ 0.11,  1.01])
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! interesting....seems to work for the values I gave you, but not for my real case values...all of them have exactly the same features of the values from the example above: >>params['ampl'] ---> array([ 3931.41287385, 412.51629703, 257.47891338, ..., 193.89134605, 192.66807322, 192.20132863]) ---> index = np.where(np.in1d(params['ampl'], [3931.41287385]))[0] ---> index ---> array([], dtype=int64)
That could be due to the float number inaccuracy. You can test this by params['ampl'][0] == 3931.41287385 to seem if they are equal. Some tolerance has to be introduced here.
Thanks...unfortunately same behavior index = np.where(params['ampl'][0] == 3931.41287385) ---> index ---> (array([], dtype=int64),)
No, I mean literally print(params['ampl'][0] == 3931.41287385), you will see that it should be true but it returns false due to float number inaccuracy.
Thanks Psidom for your help! You were right, that was a inaccuracy in the float number, I used np.around to reduce the number to 3 decimals and worked perfect!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.