0

I have an array A of size 100 which might have repeating elements in it. I have another array B of size 10 which have unique elements in it. All elements of B are present in A and vice versa. I have another array C corresponding to B where each element of C is corresponding to the element in B.

I want to create an array A2 composed of elements of C, such that I can achieve the following:

import numpy as np
A = np.array([1,1,4,5,5,6])
B = np.array([4,6,5,1)])
C = np.array(['A','B','C','D')])

I want to create A2 such that:

A2 = np.array(['D','D','A','C','C','B'])

A2 has elements from C based on matching index of elements of B in A.

1
  • Does it have to be numpy? Plain old Python seems more than enough to handle this. Commented Jun 10, 2016 at 14:36

3 Answers 3

1

No need for numpy. Just zip the B and C arrays to a dict and map the values of A:

>>> btoc = dict(zip(B, C))
>>> A2 = np.array(map(btoc.get, A))
>>> A2
array(['D', 'D', 'A', 'C', 'C', 'B'], dtype='|S1')
Sign up to request clarification or add additional context in comments.

Comments

1

Here's a NumPythonic approach using np.searchsorted -

sidx = B.argsort()
out = C[sidx[np.searchsorted(B,A,sorter = sidx)]]

Sample run -

In [17]: A = np.array([1,1,4,5,5,6])
    ...: B = np.array([4,6,5,1])
    ...: C = np.array(['A','B','C','D'])
    ...: 

In [18]: sidx = B.argsort()

In [19]: C[sidx[np.searchsorted(B,A,sorter = sidx)]]
Out[19]: 
array(['D', 'D', 'A', 'C', 'C', 'B'], 
      dtype='|S1')

Comments

0

The numpy_indexed package (disclaimer: I am its author) contains functionality to do this in a single call; npi.indices, which is a vectorized equivalent of list.index.

import numpy as np
A = np.array([1,1,4,5,5,6])
B = np.array([4,6,5,1])
C = np.array(['A','B','C','D'])

import numpy_indexed as npi
i = npi.indices(B, A)
print(C[i])

Performance should be similar to the solution of Divakar, since it operates along the same lines; but all wrapped up in a convenient package with tests and all.

2 Comments

pip install numpy_indexed gives error: no module named yaml
thanks for the feedback, but strange; I thought id fixed that already... running pip install pyyaml first should fix that though.

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.