このスレで議論されている話題。
大きな整数をnumpyの関数に渡すと、 AttributeError: ‘int’ object has no attribute のエラーになることがある。
例えば、
>>> import numpy as np
>>> np.log2(2**63)
63.0
>>> np.log2(2**64)
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'int' object has no attribute 'log2'
これはnumpy内部のデータ型がuint64からobjectへと変わることで起こっているようだ。
>>> np.array([2**63])
array([9223372036854775808], dtype=uint64)
>>> np.array([2**64])
array([18446744073709551616], dtype=object)
以下のようにすれば回避可能:
>>> np.log2(float(2**64))
64.0
>>> np.log2(2.**64)
64.0
>>> import math
>>> math.log2(2**64)
64.0
コメント