Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

python - Why fancy indexing is not same as slicing in numpy?

I have been learning Fancy indexing but when I observed the behavior of the following code I got a couple of questions...

According to my understanding,
Fancy Indexing is:

ndArray[ [0,1,2] ] i.e. passing a list of rows / columns

and

Slicing is:
ndArray[ 0:3 ] i.e. giving a range of rows / columns


Now, the problem

A numpy array,

arr = [ [1,2,3],
        [4,5,6],
        [7,8,9] ]

When I try fancy indexing:

arr[ [0,1], [1,2] ]
>>> [2, 6]

And when slice it,

arr[:2, 1:]
>>> [ [2, 3],
      [5, 6] ]

Essentially both of them should return the two-dimension array as both of them mean the same, as they are used interchangeably!

:2 should be equivalent to [0,1]           #For rows
1: should be equivalent to [1,2]           #For cols

The question: Why Fancy indexing is not returning as the slice notation? And how to achieve that?


Please enlighten me. Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Fancy indexing and slicing behave differently by definition / by numpy specification.

So, instead of questioning why that is so, it is better to:

  • Be able to recognize / distinguish / tell them apart (i.e., have a clear understanding of when does the indexing become fancy indexing, and when is it slicing).

  • Be aware of the differences in their semantics (outcomes).

In your example:

In the case of fancy indexing, the indices generated for the two axes are combined "in tandem" (similar to how the zip function combines two input sequences "in tandem". (In the words of the official numpy documentation, the two index arrays are "iterated together"). We are passing the list [0, 1] for indexing the array on axis 0, and passing the list [1, 2] for indexing the array on axis 1. The index 0 from the index array [0, 1] is combined only with the corresponding index 1 of the index array [1, 2]. Similarly, the index 1 of the index array [0, 1] is combined only with the corresponding index 2 of the index array [1, 2]. In other words, the index arrays do not combine with each other in a many-to-many fashion. All this was about fancy indexing.

In the case of slicing, the slice :2 that is specified for axis 0 conceptually generates indices '0' and '1' for axis 0; and the slice 1: specified for axis 1 conceptually generates indices 1 and 2 for axis 1. But these generated indices combine in a many-to-many fashion, unlike in the case of fancy indexing. So, they produce four combinations rather than just two.

So, the crucial difference in the defined semantics of fancy indexing and slicing is that in the case of fancy indexing, the fancy index arrays are iterated together.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...