I'm a bit confused about the indexing of numpy. Assume the following example:
>>> import numpy as np
>>> x = np.arange(10)
>>> x.shape = (2,5)
>>> x
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> x[0:-1]
array([[0, 1, 2, 3, 4]])
>>> x[1:-1]
array([], shape=(0, 5), dtype=int64)
>>> x[1:]
array([[5, 6, 7, 8, 9]])
What I'm confused about, I can get the first row as 2D-array using x[0:-1]
. But what does the -1
actually means in terms of indices? I would have thought, that calling x[1:-1]
would then give me the second row, but instead if returns me an empty array, and to get what I want I need to use x[1:]?
I'm a little bit confused. Thanks for the help
[1:-1]
: from the second (item/row) to the last (=second) item/row: that will lead to an empty array.x = [1, 2]
, and index that in the same way: see what you get. It just so happens in your case, that the numbers1
and2
are replaced by arrays of length 5.