运行
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
.