NumPy 使用索引列表选择每行特定的列索引 [英] NumPy selecting specific column index per row by using a list of indexes
问题描述
我正在努力选择 NumPy 矩阵每行的特定列.
假设我有以下矩阵,我将其称为 X
:
[1, 2, 3][4, 5, 6][7, 8, 9]
我还有一个 list
的每一行的列索引,我称之为 Y
:
[1, 0, 2]
我需要获取值:
<代码>[2][4][9]
代替带有索引 Y
的 list
,我还可以生成一个与 X
形状相同的矩阵,其中每一列都是一个 bool
/int
0-1 范围内的值,表示此列是否为必填列.
[0, 1, 0][1, 0, 0][0, 0, 1]
我知道这可以通过迭代数组并选择我需要的列值来完成.但是,这将在大量数据上频繁执行,这就是它必须尽可能快地运行的原因.
我因此想知道是否有更好的解决方案?
如果你有一个布尔数组,你可以像这样直接选择:
<预><代码>>>>a = np.array([真、真、真、假、假])>>>b = np.array([1,2,3,4,5])>>>乙[一]数组([1, 2, 3])为了配合您的初始示例,您可以执行以下操作:
<预><代码>>>>a = np.array([[1,2,3], [4,5,6], [7,8,9]])>>>b = np.array([[False,True,False],[True,False,False],[False,False,True]])>>>[b]数组([2, 4, 9])您也可以添加一个 arange
并对其进行直接选择,但这取决于您生成布尔数组的方式以及您的代码看起来像 YMMV.
希望对您有所帮助,如果您还有其他问题,请告诉我.
I'm struggling to select the specific columns per row of a NumPy matrix.
Suppose I have the following matrix which I would call X
:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
I also have a list
of column indexes per every row which I would call Y
:
[1, 0, 2]
I need to get the values:
[2]
[4]
[9]
Instead of a list
with indexes Y
, I can also produce a matrix with the same shape as X
where every column is a bool
/ int
in the range 0-1 value, indicating whether this is the required column.
[0, 1, 0]
[1, 0, 0]
[0, 0, 1]
I know this can be done with iterating over the array and selecting the column values I need. However, this will be executed frequently on big arrays of data and that's why it has to run as fast as it can.
I was thus wondering if there is a better solution?
If you've got a boolean array you can do direct selection based on that like so:
>>> a = np.array([True, True, True, False, False])
>>> b = np.array([1,2,3,4,5])
>>> b[a]
array([1, 2, 3])
To go along with your initial example you could do the following:
>>> a = np.array([[1,2,3], [4,5,6], [7,8,9]])
>>> b = np.array([[False,True,False],[True,False,False],[False,False,True]])
>>> a[b]
array([2, 4, 9])
You can also add in an arange
and do direct selection on that, though depending on how you're generating your boolean array and what your code looks like YMMV.
>>> a = np.array([[1,2,3], [4,5,6], [7,8,9]])
>>> a[np.arange(len(a)), [1,0,2]]
array([2, 4, 9])
Hope that helps, let me know if you've got any more questions.
这篇关于NumPy 使用索引列表选择每行特定的列索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!