mapping two numpy arrays

0 votes
I have two numpy arrays A and B.

A = np.array ([[ 1  3] [ 2  3]  [ 2  1] ])

B = np.array([(1, 'Alpha'), (2, 'Beta'), (3, 'Gamma')]
 

How can I map A with B in order to get something like:

result = np.array ([[ 'Alpha'  'Gamma'] [ 'Beta'  'Gamma']  ['Beta'  'Alpha'] ])
 

I have tried this : map(B['f1'],A)  but I am getting TypeError: 'numpy.ndarray'  object is not callable. How can I solve this issue?
May 10, 2019 in Python by ana1504.k
• 7,910 points
2,994 views

1 answer to this question.

0 votes
Here's a NumPythonic vectorized approach -

B[:,1][(A == B[:,0].astype(int)[:,None,None]).argmax(0)]
Sample run on a generic case -

In [118]: A
Out[118]:
array([[4, 3],
       [2, 3],
       [2, 4]])

In [119]: B
Out[119]:
array([['3', 'Alpha'],
       ['4', 'Beta'],
       ['2', 'Gamma']],
      dtype='|S5')

In [120]: B[:,1][(A == B[:,0].astype(int)[:,None,None]).argmax(0)]
Out[120]:
array([['Beta', 'Alpha'],
       ['Gamma', 'Alpha'],
       ['Gamma', 'Beta']],
      dtype='|S5')
answered May 10, 2019 by SDeb
• 13,300 points

Related Questions In Python

0 votes
0 answers

Concatenate two numpy arrays in the 4th dimension

I got two numpy arrays in three ...READ MORE

Aug 1, 2022 in Python by krishna
• 2,820 points
467 views
0 votes
0 answers

Concatenating two one-dimensional NumPy arrays

How do I concatenate two one-dimensional arrays ...READ MORE

Nov 16, 2022 in Python by Ashwini
• 5,430 points
628 views
0 votes
1 answer

Numpy: Multiplying large arrays with dtype=int8 is SLOW

Unfortunately, the "engine" behind the scenes is ...READ MORE

answered May 9, 2018 in Python by charlie_brown
• 7,720 points
1,275 views
0 votes
1 answer

Combining numpy arrays

There is concatenation function for numpy arrays ...READ MORE

answered Jul 3, 2018 in Python by Hamartia's Mask
• 1,580 points
1,127 views
+1 vote
2 answers

View onto a numpy array?

 just index it as you normally would. ...READ MORE

answered Oct 18, 2018 in Python by roberto
1,035 views
0 votes
1 answer
0 votes
1 answer

Printing a large numpy array

numpy.set_printoptions(threshold='nan') READ MORE

answered Jul 20, 2018 in Python by Nietzsche's daemon
• 4,260 points
1,741 views
0 votes
1 answer

NaN variable without NumPy

Yes, It is possible to use the ...READ MORE

answered Nov 26, 2018 in Python by SDeb
• 13,300 points
1,934 views
0 votes
1 answer

Dictionary in NumPy array

You have a 0-dimensional array of object ...READ MORE

answered Jan 18, 2019 in Python by SDeb
• 13,300 points
8,615 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP