numpy array slicing unxpected results

后端 未结 3 1732
不知归路
不知归路 2021-01-23 07:17

I don\'t understand the behavior below. numpy arrays can generally be accessed through indexing, so [:,1] should be equivalent to [:][1], or so I thought. Could someone explain

3条回答
  •  青春惊慌失措
    2021-01-23 07:56

    Those two forms of indexing are not the same. You should use [i, j] and not [i][j]. Even where both work, the first will be faster (see this question).

    Using two indices [i][j] is two operations. It does the first index and then does the second on the result of the first operation. [:] just returns the entire array, so your first one is equivalent to array[1]. Since only one index is passed, it assumed to refer to the first dimension (rows), so this means "get row 1". Using one compound index [i, j] is a single operation that uses both indexing conditions at once, so array[:, 1] returns "all rows, column 1".

提交回复
热议问题