Returns values ​​from an array based on common value indices in two other arrays

import numpy as np a=np.random.randint(0,200,100)#rand int array b1=np.random.randint(0,100,50) b2=b1**3 c=[] 

I have a problem, I think it should be easy, but I can’t find a solution, I want to find the corresponding values ​​in two arrays, and then use the indices of one of them to search for values ​​in another array

 for i in range(len(a)): for j in range(len(b1)): if b1[j]==a[i]: c.append(b2[j]) c=np.asarray(c) 

Obviously, the above method works, but it is very slow, and this is just an example, in the work on which I actually do a, b1, b2, there are more than 10,000 elements.

Any faster solutions?

+5
source share
2 answers

np.in1d(b1, a) returns a boolean array indicating whether each element is found from b1 to a .

If you want to get the values ​​in b2 that correspond to the indices of the common values ​​in a and b1 , you can use a boolean array to index b2 :

 b2[np.in1d(b1, a)] 

Using this function should be much faster as for loops go to the level of NumPy's internal routines.

+6
source

You can use numpy.intersect1d to get the intersection between 1d arrays. Please note that when you can find an intersection, “indexes are needed or use them to find yourself again !!!”

 >>> a=np.random.randint(0,200,100) >>> b1=np.random.randint(0,100,50) >>> >>> np.intersect1d(b1,a) array([ 3, 9, 17, 19, 22, 23, 37, 53, 55, 58, 67, 85, 93, 94]) 

You may notice that using intersection is a more efficient way for a[np.in1d(a, b1)] in addition to calling in1d . The python function is forced to do additional indexing in order to better understand the following test:

 import numpy as np s1=""" import numpy as np a=np.random.randint(0,200,100) b1=np.random.randint(0,100,50) np.intersect1d(b1,a) """ s2=""" import numpy as np a=np.random.randint(0,200,100) b1=np.random.randint(0,100,50) a[np.in1d(a, b1)] """ print ' first: ' ,timeit(stmt=s1, number=100000) print 'second : ',timeit(stmt=s2, number=100000) 

Result:

  first: 3.69082999229 second : 7.77609300613 
+1
source

All Articles