One of Python's strengths is its uniform application of straightforward principles. Numpy indexing, like all indexing in Python, passes a single argument to the indexed object's (i.e., the array's) __getitem__
method, and numpy arrays were one of the primary justifications for the slicing mechanism (or at least one of its very early uses).
When I'm trying to understand new behaviours I like to start with a concrete and comprehensible example, so rather than 10x100 random values I'll start with a one-dimensional 4-element vector and work up to 3x4, which should be big enough to understand what's going on.
simple = np.array([1, 2, 3, 4])
train = np.array([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]])
The interpreter shows these as
array([1, 2, 3, 4])
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
The expression simple[x]
is equivalent to (which is to say the interpreter ends up executing) simple.__getitem__(x)
under the hood - note this call takes a single argument.
The numpy array's __getitem__
method implements indexing with an integer very simply: it selects a single element from the first dimension. So simple[1]
is 2
, and train[1]
is array([5, 6, 7, 8])
.
When __getitem__
receives a tuple as an argument (which is how Python's syntax interprets expressions like array[x, y, z]
) it applies each element of the tuple as an index to successive dimensions of the indexed object. So result = train[1, 2]
is equivalent (conceptually - the code is more complex in implementation) to
temp = train[1] # i.e. train.__getitem__(1)
result = temp[2] # i.e. temp.__getitem__(2)
and sure enough we find that result
comes out at 7
. You could think of array[x, y, z]
as equivalent to array[x][y][z]
.
Now we can add slicing to the mix. Expressions containing a colon can be regarded as slice literals (I haven't seen a better name for them), and the interpreter creates slice objects for them. As the documentation notes, a slice object is mostly a container for three values, start, stop and slice, and it's up to each object's __getitem__
method how it interprets them. You might find this question helpful to understand slicing further.
With what you now know, you should be able to understand the answer to your first question.
result = train[:-1, 1:-1]
will call train.__getitem__
with a two-element tuple of slices. This is equivalent to
temp = train[:-1]
result = temp[..., 1:-1]
The first statement can be read as "set temp
to all but the last row of train
", and the second as "set result
to all but the first and last columns of temp
". train[:-1]
is
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
and applying the [1:-1]
subscripting to the second dimension of that array gives
array([[2, 3],
[6, 7]])
The ellipsis on the first dimension of the temp
subscript says "pass everything," so the subscript expression
[...]can be considered equivalent to
[:]. As far as the
Nonevalues are concerned, a slice has a maximum of three data points: _start_, _stop_ and _step_. A
Nonevalue for any of these gives the default value, which is
0for _start_, the length of the indexed object for _stop_, and
1for _step. So
x[None:None:None]is equivalent to
x[0:len(x):1]which is equivalent to
x[::]`.
With this knowledge under your belt you should stand a bit more chance of understanding what's going on.