def _safe_resize(a, shape):
    """ Resize an ndarray safely, using minimal memory """
    try:
        a.resize(shape)
    except ValueError:
        a = a.copy()
        a.resize(shape, refcheck=False)
    return a
