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 in Python by ana1504.k
• 7,870 points
213 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 by SDeb
• 13,160 points

Related Questions In Python

0 votes
1 answer

Numpy: Multiplying large arrays with dtype=int8 is SLOW

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

answered May 9, 2018 in Python by charlie_brown
• 7,720 points
74 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
123 views
0 votes
1 answer

Numpy: Multiplying large arrays with dtype=int8 is SLOW

Unfortunately, as correctly underlined in the comments, the ...READ MORE

answered Sep 5, 2018 in Python by Priyaj
• 56,900 points
96 views
0 votes
1 answer

Save NumPy arrays to File

Try this: np.savetxt('file.txt',arr,delimiter=' ') will save to a text ...READ MORE

answered May 24 in Python by Jason
34 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
52 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
112 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,160 points
71 views
0 votes
1 answer

Dictionary in NumPy array

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

answered Jan 18 in Python by SDeb
• 13,160 points
1,072 views