This question already has an answer here:
这个问题在这里已有答案:
- slicing arrays in numpy/scipy 3 answers
- 在numpy / scipy 3答案中切片数组
I'd like to extract a numpy array with a specified size from a numpy 2d array--essentially I want to crop the array. For example, if have a numpy array like this:
我想从一个numpy 2d数组中提取一个具有指定大小的numpy数组 - 基本上我想裁剪数组。例如,如果有一个像这样的numpy数组:
([1,2,3],
[4,5,6],
[7,8,9])
I'd like to extract a 2x2 from it and the result should be:
我想从中提取2x2,结果应该是:
([1,2],
[4,5])
How can I do that?
我怎样才能做到这一点?
1 个解决方案
#1
13
Given this array:
给定这个数组:
>>> a
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
You can slice it along both dimensions:
您可以沿两个维度切片:
>>> a[:2,:2]
array([[1, 2],
[4, 5]])
#1
13
Given this array:
给定这个数组:
>>> a
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
You can slice it along both dimensions:
您可以沿两个维度切片:
>>> a[:2,:2]
array([[1, 2],
[4, 5]])