I want to find the minimum of N numpy matrices elementwise (but with a twist, read till the end). To show, I create 3 numpy matrices as follows:
JavaScript
x
16
16
1
>>> a = np.random.randint(100, size=(3,3))
2
>>> b = np.random.randint(100, size=(3,3))
3
>>> c = np.random.randint(100, size=(3,3))
4
>>> a
5
array([[79, 7, 71],
6
[14, 34, 68],
7
[98, 97, 6]])
8
>>> b
9
array([[28, 25, 95],
10
[69, 46, 39],
11
[90, 11, 21]])
12
>>> c
13
array([[56, 3, 67],
14
[44, 41, 44],
15
[66, 25, 42]])
16
I except my output d
to be:
JavaScript
1
5
1
d = array([[28, 3, 67],
2
[14, 34, 39],
3
[66, 11, 6]])
4
5
I also need to retain the information from where does the each element in the d matrix is coming from. So if I label a, b, c as class 0, 1, 2. In the end I need to have a m
matrix like this:
JavaScript
1
5
1
m = array([[1, 2, 2],
2
[0, 0, 1],
3
[2, 1, 0]])
4
5
I prefere no-loops based numpy approach.
Advertisement
Answer
To find the minimum numbers:
JavaScript
1
2
1
d = np.min([a, b, c], axis=0)
2
And their origins:
JavaScript
1
2
1
m = np.argmin([a, b, c], axis=0)
2