Python 如何从二维 numpy 数组中删除第一行和最后一行和列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29176339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I remove the first and last rows and columns from a 2D numpy array?
提问by BH2017
I'd like to know how to remove the first and last rows and columns from a 2D array in numpy. For example, say we have a (N+1) x (N+1)
matrix called H
then in MATLAB/Octave, the code I'd use would be:
我想知道如何从 numpy 中的二维数组中删除第一行和最后一行和列。例如,假设我们在 MATLAB/Octave 中有一个(N+1) x (N+1)
称为H
then的矩阵,我将使用的代码是:
Hsub = H(2:N,2:N);
What's the equivalent code in Numpy? I thought that np.reshape
might do what I want but I'm not sure how to get it to remove just the target rows as I think if I reshape to a (N-1) x (N-1)
matrix, it'll remove the last two rows and columns.
Numpy 中的等效代码是什么?我认为这np.reshape
可能会做我想做的事,但我不确定如何让它只删除目标行,因为我认为如果我重塑为(N-1) x (N-1)
矩阵,它将删除最后两行和两列。
采纳答案by rayryeng
How about this?
这个怎么样?
Hsub = H[1:-1, 1:-1]
The 1:-1
range means that we access elements from the second index, or 1
, and we go up to the second last index, as indicated by the -1
for a dimension. We do this for both dimensions independently. When you do this independently for both dimensions, the result is the intersection of how you're accessing each dimension, which is essentially chopping off the first row, first column, last row and last column.
的1:-1
范围的装置,我们访问元素从所述第二索引,或者1
,我们去到第二最后一个索引,如通过指示-1
一个维度。我们独立地对两个维度执行此操作。当您对两个维度独立执行此操作时,结果是您访问每个维度的方式的交集,这实质上是截断第一行、第一列、最后一行和最后一列。
Remember, the ending index is exclusive, so if we did 0:3
for example, we only get the first threeelements of a dimension, not four.
请记住,结束索引是exclusive 的,所以如果我们这样做0:3
,我们只会得到一个维度的前三个元素,而不是四个。
Also, negative indices mean that we access the array from the end. -1
is the last value to access in a particular dimension, but because of the exclusivity, we are getting up to the second last element, not the last element. Essentially, this is the same as doing:
此外,负索引意味着我们从末尾访问数组。-1
是特定维度中要访问的最后一个值,但由于排他性,我们将到达倒数第二个元素,而不是最后一个元素。本质上,这与执行以下操作相同:
Hsub = H[1:H.shape[0]-1, 1:H.shape[1]-1]
... but using negative indices is much more elegant. You also don't have to use the number of rows and columns to extract out what you need. The above syntax is dimension agnostic. However, you need to make sure that the matrix is at least 3 x 3, or you'll get an error.
...但使用负指数要优雅得多。您也不必使用行数和列数来提取您需要的内容。上面的语法是维度不可知的。但是,您需要确保矩阵至少为 3 x 3,否则会出错。
Small bonus
小红包
In MATLAB / Octave, you can achieve the same thing without using the dimensions by:
在 MATLAB / Octave 中,您可以通过以下方式在不使用维度的情况下实现相同的目的:
Hsub = H(2:end-1, 2:end-1);
The end
keyword with regards to indexing means to get the last element for a particular dimension.
end
关于索引的关键字意味着获取特定维度的最后一个元素。
Example use
示例使用
Here's an example (using IPython):
这是一个示例(使用 IPython):
In [1]: import numpy as np
In [2]: H = np.meshgrid(np.arange(5), np.arange(5))[0]
In [3]: H
Out[3]:
array([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]])
In [4]: Hsub = H[1:-1,1:-1]
In [5]: Hsub
Out[5]:
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
As you can see, the first row, first column, last row and last column have been removed from the source matrix H
and the remainder has been placed in the output matrix Hsub
.
如您所见,第一行、第一列、最后一行和最后一列已从源矩阵中删除,H
其余部分已放置在输出矩阵中Hsub
。