user1051505
user1051505

Reputation: 962

Comparing 2 arrays using numpy and allocating values to a third array

I have

A = numpy.array([[ 0.52241976,  0.50960677,  0.34597965]])
B = numpy.array([[0.5, 0.5, 0.5]])

I am looking for a C that does

if (A > B):
C[i] = 1

Expected C: [[ 1, 1, 0]]

How do I do that?

Edit: I have started python today. So I am early beginner in Python

Upvotes: 1

Views: 42

Answers (2)

Daniel F
Daniel F

Reputation: 14399

For this specific case you could just do

(A > B).astype(int)

As booleans convert to int as 0 and 1

Upvotes: 0

Ghilas BELHADJ
Ghilas BELHADJ

Reputation: 14106

np.where is made for this purpose

C = numpy.where(A > B, 1, 0)

Upvotes: 3

Related Questions