[英]find argmin of 3 vectors in numpy
I have three vectors我有三个向量
a = np.arange(3)
b = np.arange(2,-1,-1)
c = np.ones((3,))
and their minimum values element-wise via以及它们的最小值逐元素通过
np.minimum(a, b, c)
which is: m = [0. 1. 0.]
即:
m = [0. 1. 0.]
m = [0. 1. 0.]
I want to find from which array these values came, in my example I want input like: [0. x. 1.]
我想找到这些值来自哪个数组,在我的示例中我想要输入:
[0. x. 1.]
[0. x. 1.]
[0. x. 1.]
because first element in m
came from a
and last element in m
came from b
[0. x. 1.]
因为m
中的第一个元素来自a
而m
中的最后一个元素来自b
You can use the argmin method with the axis
argument:您可以将argmin方法与
axis
参数一起使用:
>>> import numpy as np
>>> a = np.arange(3)
>>> b = np.arange(2,-1,-1)
>>> c = np.ones((3,))
>>> a
array([0, 1, 2])
>>> b
array([2, 1, 0])
>>> c
array([1., 1., 1.])
>>> all = np.stack([a, b, c])
>>> all
array([[0., 1., 2.],
[2., 1., 0.],
[1., 1., 1.]])
>>> all.min(axis=0)
array([0., 1., 0.])
>>> all.argmin(axis=0)
array([0, 0, 1])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.