What are the rules for comparing numpy arrays using ==?
NumPy tries to broadcast the two arrays to compatible shapes before comparison. If the broadcasting fails, False is currently returned. In the future,
The equality operator
==
will in the future raise errors like np.equal if broadcasting or element comparisons, etc. fails.
Otherwise, a boolean array resulting from the element-by-element comparison is returned. For example, since x
and np.array([1])
are broadcastable, an array of shape (10,) is returned:
In [49]: np.broadcast(x, np.array([1])).shape
Out[49]: (10,)
Since x
and np.array([[1,3],[2]])
are not broadcastable, False
is returned by x == np.array([[1,3],[2]])
.
In [50]: np.broadcast(x, np.array([[1,3],[2]])).shape
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-50-56e4868cd7f7> in <module>()
----> 1 np.broadcast(x, np.array([[1,3],[2]])).shape
ValueError: shape mismatch: objects cannot be broadcast to a single shape
It's possible that what's confusing you is that:
some broadcasting is going on.
you appear to have an older version of numpy.
x == np.array([[1],[2]])
is broadcasting. It compares x
to each of the first and second arrays; as they are scalars, broadcasting implies that it compares each element of x
to each of the scalars.
However, each of
x == np.array([1,2])
and
x == np.array([[1,3],[2]])
can't be broadcast. By me, with numpy
1.10.4, this gives
/usr/local/bin/ipython:1: DeprecationWarning: elementwise == comparison failed; this will raise an error in the future.
#!/usr/bin/python
False