简体   繁体   English

在 Numpy / Scipy 中切片数组

[英]Slicing arrays in Numpy / Scipy

I have an array like:我有一个数组,如:

a = array([[1,2,3],[3,4,5],[4,5,6]])

What's the most efficient way to slice out a 1x2 array out of this that has only the first two columns of "a"?从中切出只有前两列“a”的 1x2 数组的最有效方法是什么?

ie IE

array([[2,3],[4,5],[5,6]]) in this case.

Two dimensional numpy arrays are indexed using a[i,j] (not a[i][j] ), but you can use the same slicing notation with numpy arrays and matrices as you can with ordinary matrices in python (just put them in a single [] ):二维 numpy 数组使用a[i,j] (不是a[i][j] )进行索引,但是您可以对 numpy 数组和矩阵使用相同的切片符号,就像在 python 中使用普通矩阵一样(只需将它们放入单个[] ):

>>> from numpy import array
>>> a = array([[1,2,3],[3,4,5],[4,5,6]])
>>> a[:,1:]
array([[2, 3],
       [4, 5],
       [5, 6]])

这是你要找的吗?

a[:,1:]

To quote documentation , the basic slice syntax is i:j:k where i is the starting index, j is the stopping index, and k is the step (when k > 0 ).引用文档,基本的切片语法是i:j:k ,其中i是起始索引, j是停止索引, k是步长(当k > 0 )。

Now if i is not given, it defaults to 0 if k > 0 .现在如果i没有给出,如果k > 0 ,它默认为k > 0 Otherwise i defaults to n - 1 for k < 0 (where n is the length of the array).否则,对于k < 0i默认为n - 1 (其中n是数组的长度)。

If j is not given, it defaults to n (length of array).如果未给出j ,则默认为n (数组长度)。

That's for a one dimensional array.那是一维数组。

Now a two dimensional array is a different beast.现在二维数组是一个不同的野兽。 The slicing syntax for that is a[rowrange, columnrange] .其切片语法是a[rowrange, columnrange]

So if you want all the rows, but just the last two columns, like in your case, you do:因此,如果您想要所有行,但只想要最后两列,就像您的情况一样,您可以:

a[0:3, 1:3]

Here, " [0:3] " means all the rows from 0 to 3. and " [1:3] " means all columns from column 1 to column 3.这里,“ [0:3] ”表示从0到3的所有行,“ [1:3] ”表示从第1列到第3列的所有列。

Now as you may be wondering, even though you have only 3 columns and the numbering starts from 1, it must return 3 columns right?现在您可能想知道,即使您只有 3 列并且编号从 1 开始,它也必须返回 3 列,对吗? ie: column 1, column 2, column 3即:第 1 列、第 2 列、第 3 列

That is the tricky part of this syntax.这是这个语法的棘手部分。 The first column is actually column 0. So when you say " [1:3] ", you are actually saying give me column 1 and column 2. Which are the last two columns you want.第一列实际上是第 0 列。所以当您说“ [1:3] ”时,您实际上是在说给我第 1 列和第 2 列。这是您想要的最后两列。 (There actually is no column 3.) (实际上没有第 3 列。)

Now if you don't know how long your matrix is or if you want all the rows, you can just leave that part empty.现在如果你不知道你的矩阵有多长,或者你想要所有的行,你可以把那部分留空。 ie IE

a[:, 1:3]

Same goes for columns also.列也一样。 ie if you wanted say, all the columns but just the first row, you would write即如果你想说,除了第一行之外的所有列,你会写

a[0:1,:]

Now, how the above answer a[:,1:] works is because when you say " [1:] " for columns, it means give me everything except for column 0, and till the end of all the columns.现在,上面的答案a[:,1:]是如何工作的,因为当你对列说“ [1:] ”时,这意味着给我除了第 0 列之外的所有内容,直到所有列的末尾。 ie empty means 'till the end'.即空意味着“直到最后”。

By now you must realize that anything on either side of the comma is all a subset of the one dimensional case I first mentioned above.现在你必须意识到逗号两边的任何东西都是我上面第一次提到的一维情况的子集。 ie if you want to specify your rows using step sizes you can write即如果你想使用步长指定你的行,你可以写

a[::2,1]

Which in your case would return在你的情况下哪个会返回

array([[2, 3],
       [5, 6]])

ie a[::2,1] elucidates as: give me every other row, starting with the top most, and give me only the 2nd column.a[::2,1]阐明为:每隔一行给我,从最上面开始,只给我第二列。

This took me some time to figure out.这花了我一些时间才弄明白。 So pasting it here, just in case it helps someone.所以把它贴在这里,以防万一它对某人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM