0
import numpy as np

a = np.array([[1., 2., 3.], 
              [4., 5., 'a']], dtype=object)
b = np.array([[1.00000001, 2., 3.], 
              [4., 5., 'a']], dtype=object)
print(a == b)

actual output:

[[False  True  True]
 [ True  True  True]]

expected output (since 1.00000001 is close enough to 1):

[[True  True  True]
 [True  True  True]]

I cannot use numpy.isclose() because there is non-numerical part in the array.

吴慈霆
  • 523
  • 2
  • 15
  • You could strip off the string and convert to float: `a.ravel()[:-1].astype(float)`. `isclose` uses the difference, and also checks for floats like `inf`, which is why it needs the numeric dtype. Or you could leave the values `object` dtype, and do your own test of the differences `a.ravel()[:-1]` – hpaulj Jan 06 '22 at 04:15
  • The non-numerical part can be in any place in the array, not only in the last place. – 吴慈霆 Jan 06 '22 at 04:53

1 Answers1

0

It's a bit hacky, but you could use masks to solve it. For example, using the is_numeric_3 function from this answer (which I'll report here, too, for completeness):

def is_float(val):
        try:
            float(val)
        except ValueError:
            return False
        else:
            return True

is_numeric_3 = np.vectorize(is_float, otypes = [bool]) # return numpy array

mask_a = is_numeric_3(a)
mask_b = is_numeric_3(b)
mask = mask_a & mask_b

result = a == b
result[mask] = np.isclose(a[mask], b[mask])
Marco Spinaci
  • 1,750
  • 15
  • 22