Given the following code:
import numpy as np
mat = np.arange(1,26).reshape(5,5)
My understanding was the following lines are identical:
mat[:3][1:2]
mat[:3,1:2]
But they are not. Why?
If you only specify one dimension in your slicing syntax, only one dimension will be sliced. In NumPy, dimensions in indexing are typically separated by ",".
For a 2d array, you may substitute "row" with "dimension 1" and "column" with "dimension 2". In your example, mat[:3] slices the first 3 rows. The subsequent indexer [1:2], slices the first of those 3 rows.
With your second example, [:3, 1:2] slices rows and columns simultaneously.
You may find it helpful to look at the shapes of your results:
mat[:3].shape # (3, 5)
mat[:3][1:2].shape # (1, 5)
mat[:3,1:2].shape # (3, 1)
Your matrix:
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25]])
The first one mat[:3][1:2] will first take mat[:3] and then apply [1:2]:
mat[:3]
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]])
# mat[:3][1:2] => array([[ 6, 7, 8, 9, 10]])
While the second one (mat[:3,1:2]) states:
lines up to 3
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]])
Columns 1 to 2
array([[ 2],
[ 7],
[12]])
Conclusion, the main difference is that the first one is applying [1:2] after the [:3]
Here's why:
> mat
# output:
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25]])
> mat[:3] # you are selecting the first 3 rows
#output:
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]])
> mat[:3][1:2] # you are selecting the second row only
Output:
array([[ 6, 7, 8, 9, 10]])
> mat[:3,1:2] # you are selecting from the first 3 rows and the second column
Output:
array([[ 2],
[ 7],
[12]])
mat[x][y]is the same asmat[x, y]only ifxis a scalar, i.e. if it selects one item on the first dimension.