def unbroadcast(array):
    slices = []
for i in range(array.ndim):
    if array.strides[i] == 0:
        slices.append(slice(0, 1))
else:
    slices.append(slice(None))
return array[slices]
Un-broadcasting Numpy arrays