clarky
clarky

Reputation: 471

Confusion about Numpy notation

If a is numpy array of shape (5,3) and b is of shape (2,4), what is the shape of a[b]?

In the above equation, what does a[b] really implies? Is it multiplication?

Upvotes: 1

Views: 137

Answers (1)

wim
wim

Reputation: 362756

No, this is not any kind of multiplication at all. This is an advanced indexing. Assuming the elements of b are valid indices into rows of a, then a[b] will be a 3D array with shape (2,4,3). In your example, that means b should be an integer array with values between -5 and 4 inclusive, else indexing a[b] will raise IndexError. The result a[b] will be made up of the corresponding rows from a, stacked in depth.

This feature of numpy is documented here.

Upvotes: 2

Related Questions