Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Maximum and Minimum values from multiple arrays in Python

I have two arrays X1 and X2. I want to find maximum and minimum values from these two arrays in one step. But it runs into an error. I present the expected output.

import numpy as np

X1 = np.array([[ 527.25      ],
       [2604.89634575],
       [ 946.77085166],
       [ 816.79051828],
       [1388.77873104],
       [4633.70349825],
       [1125.9493112 ],
       [1811.67700025],
       [ 718.19141262],
       [ 640.83306256],
       [ 578.51918766],
       [ 522.02970297]])   

X2 = np.array([[ 527.25      ],
       [2604.89634575],
       [ 941.87856824],
       [ 781.29465624],
       [1388.77873104],
       [4633.70349825],
       [1125.9493112 ],
       [1811.67700025],
       [ 319.09009796],
       [ 558.12142224],
       [ 484.73489944],
       [ 473.62756082]])  

Max=max(X1,X2)
Min=min(X1,X2)

The error is

in <module>
    Max=max(X1,X2)

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

The expected output is

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

array([4633.70349825])
array([319.09009796])

>Solution :

You can use np.ufunc.reduce with multiple axis

(first find max on axis=1 from X1 , X2 then find max on axis=0 from result.)

np.maximum.reduce([X1, X2], axis=(1,0))
# array([4633.70349825])

np.minimum.reduce([X1, X2], axis=(1,0))
# array([319.09009796])

Or try this:

>>> max(max(X1), max(X2))
array([4633.70349825])

>>> min(min(X1), min(X2))
array([319.09009796])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading