numpy ndarray subclass: ufunc don't return scalar type

妖精的绣舞 提交于 2019-12-06 10:41:38
Evan

You need to override __array_wrap__ in your ndarray subclass with a function that looks like this:

def __array_wrap__(self, obj):
    if obj.shape == ():
        return obj[()]    # if ufunc output is scalar, return it
    else:
        return np.ndarray.__array_wrap__(self, obj)

__array_wrap__ is called after ufuncs to do cleanup work. In the default implementation special cases exact ndarrays (but not subclasses) to convert zero-rank arrays to scalars. At least this is true for some versions of numpy.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!