python + numpy:如果numpy.log的操作数太大,为什么会抛出属性错误? [英] python+numpy: why does numpy.log throw an attribute error if its operand is too big?
问题描述
运行
np.log(math.factorial(21))
抛出一个AttributeError: log
.这是为什么?我可以想象一个ValueError
或某种UseYourHighSchoolMathsError
,但是为什么属性错误?
throws an AttributeError: log
. Why is that? I could imagine a ValueError
, or some sort of UseYourHighSchoolMathsError
, but why the attribute error?
推荐答案
math.factorial(21)
的结果是Python长的. numpy无法将其转换为其数字类型之一,因此将其保留为dtype=object
.一元ufuncs用于对象数组的方式是,它们只是尝试在对象上调用相同名称的方法.例如
The result of math.factorial(21)
is a Python long. numpy cannot convert it to one of its numeric types, so it leaves it as dtype=object
. The way that unary ufuncs work for object arrays is that they simply try to call a method of the same name on the object. E.g.
np.log(np.array([x], dtype=object)) <-> np.array([x.log()], dtype=object)
由于Python上没有.log()
方法,因此您会得到AttributeError
.
Since there is no .log()
method on a Python long, you get the AttributeError
.
这篇关于python + numpy:如果numpy.log的操作数太大,为什么会抛出属性错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!