次のコードがあるとします。
import numpy as np
mat = np.arange(1,26).reshape(5,5)
私の理解では、次の行は同一です。
mat[:3][1:2]
mat[:3,1:2]
しかし、そうではありません。なんで?
次のコードがあるとします。
import numpy as np
mat = np.arange(1,26).reshape(5,5)
私の理解では、次の行は同一です。
mat[:3][1:2]
mat[:3,1:2]
しかし、そうではありません。なんで?
理由は次のとおりです。
> 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]])