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 arrays comparison

I have 2 pytorch tensors (single column) of 40 elements To compare them element by element I converted them to numpy arrays with a single column of 40 elements. I want to compare both arrays element by element and if the value is greater than 0.5 in one array make it 1 else 0 and convert the result again to pytorch tensor. How do I do that.

>Solution :

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

Maybe this helps:

import numpy as np
a = np.array([1, 2, 3, 4, 5])
b = np.array([1.1, 2.6, 3.3, 4.6, 5.5])
(np.abs(a-b)>0.5).astype(int)
>>> array([0, 1, 0, 1, 0])
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