ou sha
ou sha

Reputation: 13

Python how to compare the elements in 2 arrays by row

I am a beginner in Python, here's an example:

a = array ([[4,7,8],
            [8,8,8],
            [5,8,4]]) 
b = array([[1,1,7],
           [2,6,9],
           [2,3,4]])
the output would be [1,1,0]

I want to compare how many elements in b's first row is bigger than the elements in a's first row first elements. And apply the same comparison to every row. The comparison has to be the same row in 2 arrays.The original data shape as(297,6940).Is there any easy way to do with this?Thanks!!!

Upvotes: 1

Views: 605

Answers (2)

I don't know if I understood good, but I did a example to try explain it to you:

https://repl.it/@Gilles_Medeiros/CompareElementsArray

You'll iterate on first array and compare each first element of the row with all elements in the row in the second array. It's not the best solution, but it's easy to understand.

Upvotes: 0

Space Impact
Space Impact

Reputation: 13255

Use direct array comparison and sum them along rows as:

(a<b).sum(axis=1)

array([0, 1, 0])

a<b
array([[False, False, False],
       [False, False,  True],
       [False, False, False]])

Upvotes: 1

Related Questions