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

NumPy: reduce < along axis 1

I have an array of pairs of ints. I’d like to reduce each pair using <, but np.less.reduce(..., axis=1) which I expected to work, doesn’t:

>>> np.less.reduce(np.array([[1, 2], [3, 1]]), axis=1)
array([False, False])

I wanted the result array([True, False]). This surprised me, seeing that add.reduce (yes, I know that can be just sum(...)) does what I expect:

>>> np.add.reduce(np.array([[1, 2], [3, 1]]), axis=1)
array([3, 4])

What have I misunderstood?

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

>Solution :

When doing

np.less.reduce([1,2])

it returns False, so less.reduce does not work the way you want it to here.

Why not simply:

arr = np.array([[1, 2], [3, 1]])
np.less(arr[:,0], arr[:,1])
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