Python numpy.ufunc.identity的用法
我想知道什么行Python numpy.ufunc.identity的用法,python,numpy,Python,Numpy,我想知道什么行r=ufct.identity内部函数ufunc\u减少了。它只是像r=0一样初始化操作吗 a = np.array([2,3,4,5]) b = np.array([8,5,4]) c = np.array([5,4,6,8,3]) def ufunc_reduce(ufct, *vectors): vs = np.ix_(*vectors) r = ufct.identity for v in vs: r = ufct(r,v)
r=ufct.identity
内部函数ufunc\u减少了
。它只是像r=0
一样初始化操作吗
a = np.array([2,3,4,5])
b = np.array([8,5,4])
c = np.array([5,4,6,8,3])
def ufunc_reduce(ufct, *vectors):
vs = np.ix_(*vectors)
r = ufct.identity
for v in vs:
r = ufct(r,v)
return r
ufunc_reduce(np.add,a,b,c)
是的,对于ufunc
np.add
,ufct.identity
是0
。但对于另一个函数,它可能是另一个函数。例如,np.multiply.identity
是1
您显示的
ufunc\u reduce
函数事先不知道将提供什么函数,因此不能硬编码为使用0
或1
。它通过检查ufunc的identity
属性来获得正确的值。常规ufunc中使用identity
属性。reduce
作为wll,