Home > Software engineering >  Numpy arrays comparison
Numpy arrays comparison

Time:12-20

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.

CodePudding user response:

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