JRR JRR - 6 months ago 36
Python Question

how to slice a subset of numpy arrays

Given this array:

>>> a
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])


How can I select
[[4,5], [7,8]]
?
a[0::2, 1:;2]
doesn't work

Answer
>>> a
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
>>> a[1:3,1:3]
array([[4, 5],
       [7, 8]])

The first 1:3 is to select row 1 & 2. The second 1:3 is to select column 1 & 2.