查找numpy数组中每行的最大值以及相同大小的另一个数组中的相应元素

时间:2022-03-08 01:41:36

I am new to Python and still cannot call myself a Python programmer. Speaking of that, please bear with me if my question does not make any sense.

我是Python新手,仍然不能称自己为Python程序员。说到这一点,如果我的问题没有任何意义,请耐心等待。

Question:

I have two numpy arrays of the same size, e.g. A and B where A.shape equals B.shape and they both equal (5,1000), and I want to find the maximum value of each row in A and the corresponding element of that in B. For instance, if in fourth row of A, maximum element index is 104 then I would like to find the 104th element of fourth row in array B and the same for the rest of the rows.

我有两个相同大小的numpy数组,例如A和B,其中A.shape等于B.shape,它们都等于(5,1000),我想找到A中每行的最大值和B中相应的元素。例如,如果在第四行A,最大元素索引是104然后我想找到数组B中第四行的第104个元素,其余的行相同。

I know I can do it by looping over the rows but I was wondering if there was a more elegant way of doing it. For example, if I were to do it in MATLAB I would write the following code:

我知道我可以通过循环遍历行来完成它,但我想知道是否有一种更优雅的方式。例如,如果我在MATLAB中这样做,我会编写以下代码:

B(bsxfun(@eq,A,max(A,[],2)))

Any help that guides me through the right direction would be much appreciated.

任何指导我通过正确方向的帮助都将非常感激。

3 个解决方案

#1


6  

Here's the numpy idiom for doing the same thing:

这是做同样事情的numpy成语:

b[np.arange(len(a)), np.argmax(a, axis=1)]

For example:

>>> a = np.array([
    [1, 2, 0],
    [2, 1, 0],
    [0, 1, 2]
    ])
>>> b = np.array([
    [1, 2, 3],
    [1, 2, 3],
    [1, 2, 3]
    ])
>>> b[np.arange(len(a)), np.argmax(a, axis=1)]
array([2, 1, 3])

#2


1  

Being a bsxfun lover, it's great to see people trying to replicate the same functionality to other programming languages. Now, bsxfun is basically a broadcasting mechanism, which exists in NumPy as well. In NumPy, it is achieved by creating singleton dimensions with np.newaxis or simply None.

作为一个bsxfun爱好者,很高兴看到人们试图将相同的功能复制到其他编程语言。现在,bsxfun基本上是一种广播机制,也存在于NumPy中。在NumPy中,它是通过使用np.newaxis或简单的None创建单例维度来实现的。

Back to the question in context, an equivalent broadcasting based solution could be implemented as shown as a sample run -

回到上下文中的问题,可以实现基于等效广播的解决方案,如示例运行所示 -

In [128]: A
Out[128]: 
array([[40, 63, 67, 65, 19],
       [85, 55, 66, 92, 88],
       [50,  1, 23,  6, 59],
       [67, 55, 46, 78,  3]])

In [129]: B
Out[129]: 
array([[78, 63, 45, 34, 81],
       [ 5, 38, 28, 61, 66],
       [ 3, 65, 16, 25, 32],
       [72,  1, 31, 75,  6]])

In [130]: B[A == A.max(axis=1)[:,None]]
Out[130]: array([45, 61, 32, 75])

#3


0  

print np.max(A[i]) This will give the highest in the i th row of a numpy matrix.

print np.max(A [i])这将给出numpy矩阵的第i行中的最高值。

#1


6  

Here's the numpy idiom for doing the same thing:

这是做同样事情的numpy成语:

b[np.arange(len(a)), np.argmax(a, axis=1)]

For example:

>>> a = np.array([
    [1, 2, 0],
    [2, 1, 0],
    [0, 1, 2]
    ])
>>> b = np.array([
    [1, 2, 3],
    [1, 2, 3],
    [1, 2, 3]
    ])
>>> b[np.arange(len(a)), np.argmax(a, axis=1)]
array([2, 1, 3])

#2


1  

Being a bsxfun lover, it's great to see people trying to replicate the same functionality to other programming languages. Now, bsxfun is basically a broadcasting mechanism, which exists in NumPy as well. In NumPy, it is achieved by creating singleton dimensions with np.newaxis or simply None.

作为一个bsxfun爱好者,很高兴看到人们试图将相同的功能复制到其他编程语言。现在,bsxfun基本上是一种广播机制,也存在于NumPy中。在NumPy中,它是通过使用np.newaxis或简单的None创建单例维度来实现的。

Back to the question in context, an equivalent broadcasting based solution could be implemented as shown as a sample run -

回到上下文中的问题,可以实现基于等效广播的解决方案,如示例运行所示 -

In [128]: A
Out[128]: 
array([[40, 63, 67, 65, 19],
       [85, 55, 66, 92, 88],
       [50,  1, 23,  6, 59],
       [67, 55, 46, 78,  3]])

In [129]: B
Out[129]: 
array([[78, 63, 45, 34, 81],
       [ 5, 38, 28, 61, 66],
       [ 3, 65, 16, 25, 32],
       [72,  1, 31, 75,  6]])

In [130]: B[A == A.max(axis=1)[:,None]]
Out[130]: array([45, 61, 32, 75])

#3


0  

print np.max(A[i]) This will give the highest in the i th row of a numpy matrix.

print np.max(A [i])这将给出numpy矩阵的第i行中的最高值。