简体   繁体   English

找到 N 个 numpy 矩阵的最小值?

[英]Finding the minimum of the N numpy matrices?

I want to find the minimum of N numpy matrices elementwise (but with a twist, read till the end).我想找到 N numpy 矩阵元素的最小值(但有一点扭曲,请读到最后)。 To show, I create 3 numpy matrices as follows:为了展示,我创建了 3 个 numpy 矩阵,如下所示:

>>> a = np.random.randint(100, size=(3,3))
>>> b = np.random.randint(100, size=(3,3))
>>> c = np.random.randint(100, size=(3,3))
>>> a
array([[79,  7, 71],
       [14, 34, 68],
       [98, 97,  6]])
>>> b
array([[28, 25, 95],
       [69, 46, 39],
       [90, 11, 21]])
>>> c
array([[56,  3, 67],
       [44, 41, 44],
       [66, 25, 42]])

I except my output d to be:我除了我的 output d是:

d = array([[28,  3, 67],
         [14, 34, 39],
         [66, 11,  6]])

I also need to retain the information from where does the each element in the d matrix is coming from.我还需要保留 d 矩阵中的每个元素来自何处的信息。 So if I label a, b, c as class 0, 1, 2. In the end I need to have a m matrix like this:所以如果我 label a, b, c as class 0, 1, 2. 最后我需要一个这样的m矩阵:

m = array([[1, 2, 2],
           [0, 0, 1],
           [2, 1, 0]])

I prefere no-loops based numpy approach.我更喜欢基于无循环的 numpy 方法。

To find the minimum numbers:要找到最小数字:

d = np.min([a, b, c], axis=0)

And their origins:以及它们的起源:

m = np.argmin([a, b, c], axis=0)

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

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