Comparing numpy array of dtype object
To make an element-wise comparison between the arrays, you can use numpy.equal()
with the keyword argument dtype=numpy.object
as in :
In [60]: np.equal(be, ce, dtype=np.object)
Out[60]:
array([[True, True, True, True,
array([ True, True, True, True, True]), True, True, True]],
dtype=object)
P.S. checked using NumPy version 1.15.2
and Python 3.6.6
edit
From the release notes for 1.15,
https://docs.scipy.org/doc/numpy-1.15.1/release.html#comparison-ufuncs-accept-dtype-object-overriding-the-default-bool
Comparison ufuncs accept dtype=object, overriding the default bool
This allows object arrays of symbolic types, which override == and
other operators to return expressions, to be compared elementwise with
np.equal(a, b, dtype=object).
To complement @kmario23's answer, what about doing
def wrpr(bools):
try:
# ints = bools.flatten().prod()
fltn_bools = np.hstack(bools)
except: # should not pass silently.
fltn_bools = np.array(wrpr(a) for a in bools)
ints = fltn_bools.prod()
if isinstance(ints, np.ndarray):
return wrpr(ints)
return bool(ints)
And finally,
>>> wrpr(np.equal(ce, be, dtype=np.object))
True
Checked using (numpy1.15.1 & Python 3.6.5) & (numpy1.15.1 & Python 2.7.13).
But still, as commented here
NumPy is designed for rigid multidimensional grids of numbers. Trying to get anything but a rigid multidimensional grid is going to be painful. (@user2357112, Jul 31 '17 at 23:10)
and/or
Moral of the story: Don't use
dtype=object
arrays. They are stunted Python lists, with worse performance characteristics, and numpy is not designed to handle the case of sequence-like containers within these object arrays. (@juanpa.arrivillaga, Jul 31 '17 at 23:38)