def unbroadcast(array):
    """
    Given an array, return a new array that is the smallest subset of the
    original array that can be re-broadcasted back to the original array.

    See https://stackoverflow.com/questions/40845769/un-broadcasting-numpy-arrays
    for more details.
    """

    if array.ndim == 0:
        return array

    array = array[tuple((slice(0, 1) if stride == 0 else slice(None))
                        for stride in array.strides)]

    # Remove leading ones, which are not needed in numpy broadcasting.
    first_not_unity = next((i for (i, s) in enumerate(array.shape) if s > 1),
                           array.ndim)

    return array.reshape(array.shape[first_not_unity:])