xpqz
xpqz

Reputation: 3737

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?

Upvotes: 0

Views: 115

Answers (1)

Learning is a mess
Learning is a mess

Reputation: 8277

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])

Upvotes: 1

Related Questions