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

Comparing array elements using mathematical symbols in Python

I have an array A. I am comparing A[i+1]=A[i] but the current output is in terms of 0’s and 1’s. Instead I want it to give in terms of < or = or >. I present the expected output.

import numpy as np

A=np.array([np.array([[0.01609   , 0.01728839, 0.01635707],
              [0.01696908, 0.01661592, 0.0163007 ],
              [0.01609   , 0.01642818, 0.01950431]]),
       np.array([[[0.01609   , 0.01728839, 0.01635707],
               [0.01696908, 0.01661592, 0.0163007 ],
               [0.01609   , 0.01642818, 0.01950431]]]),
       np.array([[[0.01609   , 0.017286  , 0.01635698],
               [0.01698125, 0.01659415, 0.01630078],
               [0.01627952, 0.01639007, 0.01951677]]])], dtype=object)


for i in range(0,len(A)-1):
    B=(A[i+1]==A[i]).astype(int)
    print([B])

The current output is

[array([[[1, 1, 1],
        [1, 1, 1],
        [1, 1, 1]]])]
[array([[[1, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]])]

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([[=, =, =],
        [=, =, =],
        [=, =, =]]])]
[array([[[=, <, <],
        [>, <, >],
        [>, <, >]]])]

>Solution :

You can do it using np.select

for i in range(len(A)-1):
    B = np.select([A[i+1]==A[i],A[i+1]>A[i],A[i+1]<A[i]], ["=",">","<"])
    print([B])
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