What is the best way to test whether an array can be broadcast to a given shape?
The \"pythonic\" approach of try
ing doesn\'t work for my case, because the
For the case of when you want to check any number of array-like objects (opposed to passing shapes), we can leverage np.nditer
for broadcasting array iteration.
def is_broadcastable(*arrays):
try:
np.nditer(arrays)
return True
except ValueError:
return False
Be aware that this only works for np.ndarray
or classes that define __array__
(which will be called).