1

I have 2 arrays one is a Large_arr and another is (a subset of it) sub_arr. I want to map the values of both the array and the matched values of the array will return indices of the large array. I have my code but it throws an error

    Traceback (most recent call last):
  File "C:/Users/hp/AppData/Local/Programs/Python/Python36/stack.py", line 36, in <module>
    if(sub_arr[i]==Large_arr[j]):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

My code is

import numpy as np 

Large_arr=np.array([[0.12046862, 0.64457892],
 [0.47522597, 0.12350968],
 [0.1649585 , 0.50135502],
 [0.20104755 ,0.15218623],
 [0.03772419 ,0.42482297],
 [0.51633778 ,0.61209002],
 [0.24848648 ,0.99651906],
 [0.47374345, 0.09990318],
 [0.58390815 ,0.19781604],
 [0.9613725  ,0.45975827],
 [0.99008266 ,0.13487207],
 [0.14410988 ,0.36196475],
 [0.81349573 ,0.55896232],
 [0.72841399 ,0.02263056],
 [0.8692731  ,0.9754183 ],
 [0.87142787 ,0.66163271],
 [0.24342035 ,0.95821073],
 [0.94218857 ,0.7220602 ],
 [0.66716105 ,0.96875209]])

sub_arr=np.array([[0.12046862, 0.64457892],
                 [0.51633778 ,0.61209002],
                 [0.99008266 ,0.13487207],
                 [0.72841399 ,0.02263056],
                 [0.24342035 ,0.95821073],
                 [0.47374345, 0.09990318],
                 [0.9613725  ,0.45975827]])



s=[]
for i in range(0,len(Large_arr)):
    for j in range(0,len(sub_arr)):
        if(sub_arr[i]==Large_arr[j]):
            s.append(j)
            print("Value of s is\n",s)
        else:
            print("Value is none\n")

Can this method be simplifed. So my output has value

S=[0,5,10,13,7,10,1] (example, index of large array where sub_array value stored)
Shivam Sharma
  • 517
  • 1
  • 5
  • 19
  • You have to show us the code that raises that exception, not different code, and give us the entire exception, not just the description string. – abarnert Jun 21 '18 at 22:00
  • @abarnert made the changes – Shivam Sharma Jun 21 '18 at 22:02
  • What do you want `if(sub_arr[i]==Large_arr[j])` to do? Check whether all of the columns are equal? Check whether any of them are equal? Somehow loop over them and do the `if `code for the ones that are equal and the `else` code for the ones that aren't? – abarnert Jun 21 '18 at 22:04
  • `all(sub_arr[i] == Large_arr[j])` ? – Jarad Jun 21 '18 at 22:04
  • Why did you delete your previous post of this question to then just ask the same thing over again? – David Jun 21 '18 at 22:05
  • This is probably a dup of [the standard question on this exception](https://stackoverflow.com/questions/34472814/use-a-any-or-a-all). – abarnert Jun 21 '18 at 22:05
  • But it's always hard to tell, because when someone can't figure out how to test all of the values even when they're reading an error message that tells them to use np.all to test all of the values, there's clearly some kind of confusion going on. (Unless they're just stupid, but that doesn't seem to be the case here.) – abarnert Jun 21 '18 at 22:07
  • I think that they are simply having us do their homework for them. They clearly haven't read any of the documentation. – David Jun 21 '18 at 22:19

3 Answers3

0

First, you have your i and j indexes swapped in your loops. Make sure that they are used to index into the corresponding arrays.

Second, you are executing code that compares the subarrays element-wise, this will return an array of booleans, which is an array, not a boolean itself, so you must add .all() to reduce it to a single boolean. .all() will be true if all elements are true, otherwise it will be false.

Third, you should be able to reduce it. I have included code below to show all three of these points.

Also, your newline isn't doing what it should be. Please make sure that you test your code before posting it and include details such as the version of python being used.

import numpy as np                                                                                             

Large_arr=np.array([[0.12046862, 0.64457892],                                                                  
 [0.47522597, 0.12350968],                                                                                     
 [0.1649585 , 0.50135502],                                                                                     
 [0.20104755 ,0.15218623],                                                                                     
 [0.03772419 ,0.42482297],                                                                                     
 [0.51633778 ,0.61209002],                                                                                     
 [0.24848648 ,0.99651906],                                                                                     
 [0.47374345, 0.09990318],                                                                                     
 [0.58390815 ,0.19781604],                                                                                     
 [0.9613725  ,0.45975827],                                                                                     
 [0.99008266 ,0.13487207],                                                                                     
 [0.14410988 ,0.36196475],                                                                                     
 [0.81349573 ,0.55896232],                                                                                     
 [0.72841399 ,0.02263056],                                                                                     
 [0.8692731  ,0.9754183 ],                                                                                     
 [0.87142787 ,0.66163271],                                                                                     
 [0.24342035 ,0.95821073],                                                                                     
 [0.94218857 ,0.7220602 ],                                                                                     
 [0.66716105 ,0.96875209]])                                                                                    

sub_arr=np.array([[0.12046862, 0.64457892],                                                                    
                 [0.51633778 ,0.61209002],                                                                     
                 [0.99008266 ,0.13487207],                                                                     
                 [0.72841399 ,0.02263056],                                                                     
                 [0.24342035 ,0.95821073],                                                                     
                 [0.47374345, 0.09990318],                                                                     
                 [0.9613725  ,0.45975827]])                                                                    



s=[]                                                                                                           
for i in range(0,len(Large_arr)):                                                                              
    for j in range(0,len(sub_arr)):                                                                            
        if((sub_arr[j]==Large_arr[i]).all()):                                                                  
            s.append(i)                                                                                        
            print("Value of s is\n",s)                                                                         
        else:                                                                                                  
            #print("Value is none\n")                                                                          
            pass                                                                                               

opt = np.isin(Large_arr,sub_arr)                                                                               

print                                                                                                          
print "Bool array"                                                                                             
print opt                                                                                                      
print                                                                                                          
print "indexes"                                                                                                
print np.where(opt[:,0])[0]
David
  • 696
  • 6
  • 19
0

You could try this:

result_indices = np.argwhere(np.in1d(Large_arr, sub_arr))

That should first apply a boolean mask and then give you the indices of the matching elements.

Please correct me if this is not what you want in the end.

meow
  • 2,062
  • 2
  • 17
  • 27
0

I think the problem can come from your

if(sub_arr[i]==Large_arr[j]):

Indeed it seems that you have two 2-dimensional arrays, so you can't specify just one.

Coincoin14
  • 95
  • 14