python 从数组中选择每行中的特定列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2111163/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:49:14  来源:igfitidea点击:

Selecting specific column in each row from array

pythonnumpy

提问by astrofrog

I am trying to select specific column elements for each row of a numpy array. For example, in the following example:

我正在尝试为 numpy 数组的每一行选择特定的列元素。例如,在以下示例中:

In [1]: a = np.random.random((3,2))
Out[1]: 
array([[ 0.75670668,  0.1283942 ],
       [ 0.51326555,  0.59378083],
       [ 0.03219789,  0.53612603]])

I would like to select the first element of the first row, the second element of the second row, and the first element of the third row. So I tried to do the following:

我想选择第一行的第一个元素、第二行的第二个元素和第三行的第一个元素。所以我尝试执行以下操作:

In [2]: b = np.array([0,1,0])

In [3]: a[:,b]

But this produces the following output:

但这会产生以下输出:

Out[3]: 
array([[ 0.75670668,  0.1283942 ,  0.75670668],
       [ 0.51326555,  0.59378083,  0.51326555],
       [ 0.03219789,  0.53612603,  0.03219789]])

which clearly is not what I am looking for. Is there an easy way to do what I would like to do without using loops?

这显然不是我要找的。有没有一种简单的方法可以在不使用循环的情况下做我想做的事情?

回答by Carlos Santos

You can use:

您可以使用:

a[np.arange(3), (0,1,0)]

in your example above.

在你上面的例子中。

回答by geo

OK, just to clarify here, lets do a simple example

好的,这里只是为了澄清,让我们做一个简单的例子

A=diag(arange(0,10,1))

gives

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

then

然后

A[0][0:4]

gives

array([0, 0, 0, 0])

that is first row, elements 0 to 3. But

那是第一行,元素 0 到 3。但是

A[0:4][1]

doesn't give the first 4 rows, the 2nd element in each. Instead we get

不给出前 4 行,每行的第二个元素。相反,我们得到

array([0, 1, 0, 0, 0, 0, 0, 0, 0, 0])

i.e the entire 2nd column.

即整个第二列。

A[0:4,1]

gives

array([0, 1, 0, 0])

I'm sure there is a very good reason for this and which makes perfect sense to programmers but for those of us uninitiated in that great religion it can be quite confusing.

我相信这有一个很好的理由,这对程序员来说很有意义,但对于我们这些不了解这个伟大宗教的人来说,这可能会令人困惑。

回答by telliott99

This isn't an answer so much as an attempt to document this a bit. For the answer above, we would have:

这不是一个答案,而是试图稍微记录一下。对于上面的答案,我们将有:

>>> import numpy as np
>>> A = np.array(range(6))
>>> A
array([0, 1, 2, 3, 4, 5])
>>> A.shape = (3,2)
>>> A
array([[0, 1],
       [2, 3],
       [4, 5]])
>>> A[(0,1,2),(0,1,0)]
array([0, 3, 4])

Specifying a list (or tuple) of individual row and column coordinates allows fancy indexingof the array. The first example in the comment looks similar at first, but the indices are slices. They don't extend over the whole range, and the shapeof the array that is returned is different:

指定单个行和列坐标的列表(或元组)允许对数组进行花哨的索引。注释中的第一个示例起初看起来很相似,但索引是切片。它们不会扩展到整个范围,并且返回的数组的形状不同:

>>> A[0:2,0:2]
array([[0, 1],
       [2, 3]])

For the second example in the comment

对于评论中的第二个例子

>>> A[[0,1],[0,1]]
array([0, 3])

So it seems that slices are different, but except for that, regardless of how indices are constructed, you can specify a tuple or list of (x-values, y-values), and recover those specific elements from the array.

所以看起来切片是不同的,但除此之外,无论索引是如何构造的,您都可以指定一个元组或 (x 值,y 值) 列表,并从数组中恢复这些特定元素。