TypeError: ‘numpy.float64’ object is not callable error typically occurs in Python when you use the “multiplication without using * sign” or “overwrite the sum() function with a numpy.float64 object“.
Reason 1: Multiplication without using * sign
import numpy as np # Define arrays x = np.array([1, 2, 3, 4, 5]) y = np.array([12, 14, 14, 19, 22]) # Attempt to multiply two arrays together mul = (x)(y) # View result print(mul)
TypeError: 'numpy.ndarray' object is not callable
How to fix it?
import numpy as np # Define arrays x = np.array([11, 21, 19, 18, 46]) y = np.array([1, 2, 3, 4, 5]) # Attempt to multiply two arrays together mul = (x)*(y) # View result print(mul)
[ 11 42 57 72 230]
Reason 2: Overwrite the sum() function with a numpy.float64 object
In this approach, errors will occur when you overwrite the built-in sum() function with a numpy.float64 object. Thus, when you later try to use the sum function to add up the numbers in the list, you get the error since numpy.float64 is not callable.
import numpy as np sum = np.float64(10.5) result = sum([1, 2, 3]) print(result)
TypeError: 'numpy.float64' object is not callable
How to fix it?
To fix this, you can simply rename the sum variable to something else:
import numpy as np my_sum = np.float64(10.5) # Rename the variable result = sum([1, 2, 3]) # Now this uses the built-in sum function print(result)
Always be cautious about overwriting built-in function names. It’s a good practice to avoid using names that are the same as built-in functions or methods.