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 2,832 views

## 1 answer to this question.

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')
• 13,300 points

## Concatenate two numpy arrays in the 4th dimension

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

## Concatenating two one-dimensional NumPy arrays

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

## Numpy: Multiplying large arrays with dtype=int8 is SLOW

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

## Combining numpy arrays

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

+1 vote

## View onto a numpy array?

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

## Dimension in python numpy

Use the .shape to print the dimensions ...READ MORE

## How to slice an array using python numpy? Is there any numpy tutorial which has covered all its operations?

Slicing is basically extracting particular set of ...READ MORE