python + numpy:为什么numpy.log如果操作数太大会抛出属性错误?

Mik*_*war 26 python numpy

运行

np.log(math.factorial(21))
Run Code Online (Sandbox Code Playgroud)

抛出一个AttributeError: log.这是为什么?我可以想象一个ValueError,或某种UseYourHighSchoolMathsError,但为什么属性错误?

Rob*_*ern 31

结果math.factorial(21)是Python很长.numpy无法将其转换为其数字类型之一,因此将其保留为dtype=object.一元ufunc对对象数组的工作方式是它们只是尝试在对象上调用同名的方法.例如

np.log(np.array([x], dtype=object)) <-> np.array([x.log()], dtype=object)
Run Code Online (Sandbox Code Playgroud)

由于.log()Python上没有方法,所以你得到了AttributeError.


小智 5

首选math.log()功能,即使在长整数上也能完成.