debroadcast#
- named_arrays.debroadcast(array, axes=None)#
Remove redundant axes introduced by broadcasting.
This is the approximate inverse of
broadcast_to(): it collapses every axis along which array is constant (all slices equal), returning the smallest array which broadcasts back to the original. In particular,na.debroadcast(na.broadcast_to(a, shape))recovers an array equal to a whenever a is not itself constant along any of its own axes.An axis is only removed if every element of array is equal along that axis, so any axis containing a NaN (which compares unequal to itself) is never removed.
- Parameters:
- Return type:
ArrayT
See also
broadcast_to()Broadcast an array to a given shape.
Examples
Broadcast a 1D array to two dimensions, then recover the original.
import named_arrays as na a = na.random.uniform(0, 1, dict(x=3)) b = na.broadcast_to(a, dict(x=3, y=4)) # ``b`` is constant along ``y``, so that axis is removed c = na.debroadcast(b) c.shape
{'x': 3}# the recovered array equals the original bool((c == a).all())
True
A subset of axes can be considered by passing axes explicitly.
na.debroadcast(b, axes="x").shape
{'x': 3, 'y': 4}