Element-wise minimum of multiple vectors in numpy

后端 未结 2 758
长发绾君心
长发绾君心 2021-01-12 03:26

I know that in numpy I can compute the element-wise minimum of two vectors with

numpy.minimum(v1, v2)

What if I have a list of vectors of e

2条回答
  •  我在风中等你
    2021-01-12 04:00

    Convert to NumPy array and perform ndarray.min along the first axis -

    np.asarray(V).min(0)
    

    Or simply use np.amin as under the hoods, it will convert the input to an array before finding the minimum along that axis -

    np.amin(V,axis=0)
    

    Sample run -

    In [52]: v1 = [2,5]
    
    In [53]: v2 = [4,5]
    
    In [54]: v3 = [4,4]
    
    In [55]: v4 = [1,4]
    
    In [56]: V = [v1, v2, v3, v4]
    
    In [57]: np.asarray(V).min(0)
    Out[57]: array([1, 4])
    
    In [58]: np.amin(V,axis=0)
    Out[58]: array([1, 4])
    

    If you need to final output as a list, append the output with .tolist().

提交回复
热议问题