dimanche 23 février 2020

Compare numpy float64 vs. scalar float in an if statement in python

I have a few arrays created using a numpy random number generator and they are of type float64 all_vec is a list of numpy arrays

I then compare them to a float value: say,

bucket = 0.3

for vec in all_vectors:
  vec_min = np.amin(vec)
  vec_max = np.amax(vec) 
  if vec_min < bucket:
    bucket1.append(vec)
   <and similarly comparing to other bucket values and adding to an appropriate list>

So, I see that bucket is float while an element in the numpy array is of type numpy.float64; I tried: vec_min.item() so I get type float and also numpy.around(vec_min, decimal=4) to reduce precision.

I also converted the scalar bucket to numpy.float64 as: np.float64(0.3)

but none of the options above seem to work in the if statement. However, when I use an if statement using a value directly like:

if 0.8537153298125766 < 0.3:
   print("false")

it works perfectly fine.

I know this sounds like a simple question, but I searched a lot and most examples are trying to compare if two values are equal in which case getting the difference between two float values is prefered.

Please let me know how I can fix this.

Aucun commentaire:

Enregistrer un commentaire