Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this the wrong error message - AttributeError: 'int' object has no attribute 'log2'? [duplicate]

Tags:

python

numpy

Running

np.log(math.factorial(21))

throws an AttributeError: log. Why is that? I could imagine a ValueError, or some sort of UseYourHighSchoolMathsError, but why the attribute error?

like image 907
Mike Dewar Avatar asked Nov 20 '22 01:11

Mike Dewar


2 Answers

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)

Since there is no .log() method on a Python long, you get the AttributeError.

like image 62
Robert Kern Avatar answered Jun 11 '23 04:06

Robert Kern


Prefer the math.log() function, that does the job even on long integers.

like image 25
florent Avatar answered Jun 11 '23 03:06

florent