Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 16420

What is happening in numpy advanced index containing slice?

$
0
0

Let's say I have 3d ndarray a, created like this:

a = np.arange(0, 24).reshape(2, 3, 4)

When I run a[1, [[0, 1], [1, 0]]], below is the result.

array([[[12, 13, 14, 15],        [16, 17, 18, 19]],       [[16, 17, 18, 19],        [12, 13, 14, 15]]])

I know this is called advanced indexing, and this is happening because 1 and [[0, 1], [1, 0]] are broadcasted into (2, 2) shape.The resultant array has (2, 2, 4) shape, this reason is also written in doc (Indexing on ndarrays)

In general, the shape of the resultant array will be the concatenation of the shape of the index array (or the shape that all the index arrays were broadcast to) with the shape of any unused dimensions (those not indexed) in the array being indexed.

What I don't understand is the behavior when advanced indexing contains slice. For example, when I run a[1, 0:1, [[0, 1], [1, 0]]], I get below:

array([[[12],        [13]],       [[13],        [12]]])

The shape is (2, 2, 1). On broadcasting, 1, 0:1, [[0, 1], [1, 0]] should be indexed into (2, 2) indexing array, so I at first thought this resultant array's shape will be (2, 2). But it is actually (2, 2, 1). I want to know the exact rule how resultant array's shape is determined in advanced indexing .


Viewing all articles
Browse latest Browse all 16420

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>