Ошибка numpy float64 object is not callable

So, what im trying to do is get certain numbers from certain positions in a array of a given > range and put them into an equation

yy = arange(4)
xx = arange(5)
Area = ((xx[2] - xx[1])(yy[2] + yy[1])) / 2

I try to run it and I get this..

----> ((xx[2] - xx[1])(yy[2] + yy[1])) / 2
TypeError: 'numpy.int64' object is not callable

I get error.. how can I use certain numbers in an array and put them into an equation?

Burhan Khalid's user avatar

Burhan Khalid

169k18 gold badges244 silver badges282 bronze badges

asked Nov 7, 2013 at 4:49

user2963265's user avatar

Python does not follow the same rules as written math. You must explicitly indicate multiplication.

Bad:

(a)(b)

(unless a is a function)

Good:

(a) * (b)

Community's user avatar

answered Nov 7, 2013 at 4:53

Ignacio Vazquez-Abrams's user avatar

This error also occurs when your function has the same name as your return value

def samename(a, b):
    samename = a*b
    return samename

This might be a super rookie mistake, I am curious how often this answer will be helpful.

answered Aug 14, 2019 at 15:25

mrk's user avatar

mrkmrk

7,9443 gold badges55 silver badges78 bronze badges

1

You are missing * when multiplying, try:

import numpy as np
yy = np.arange(4)
xx = np.arange(5)
Area = ((xx[2] - xx[1])*(yy[2] + yy[1])) / 2

answered Nov 7, 2013 at 4:53

Akavall's user avatar

AkavallAkavall

82.1k51 gold badges205 silver badges249 bronze badges

This could happen because you have overwritten the name of the function that you attempt to call.

For example:

def x():
    print("hello world")
...
x = 10.5
...
x()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
      2     print("hello world")
      3 x = 10.5
----> 4 x()

TypeError: 'float' object is not callable

answered Jun 16, 2020 at 19:37

Kratos's user avatar

KratosKratos

1063 bronze badges

  • Редакция Кодкампа

17 авг. 2022 г.
читать 1 мин


Одна ошибка, с которой вы можете столкнуться при использовании Python:

TypeError : 'numpy.float64' object is not callable

Эта ошибка может возникнуть в двух разных сценариях:

  • Сценарий 1: умножение без использования знака *
  • Сценарий 2: отказ от использования функции NumPy Min

В следующих примерах показано, как исправить эту ошибку в каждом сценарии.

Сценарий 1: умножение без использования знака *

Предположим, мы пытаемся умножить два массива NumPy без использования знака умножения (*) следующим образом:

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
combo = (x)(y)

#view result
print(combo)

TypeError : 'numpy.float64' object is not callable

Мы получаем TypeError , потому что мы не использовали знак умножения (*) при попытке умножить два массива.

Чтобы избежать этой ошибки, нужно убедиться, что мы использовали знак умножения:

import numpy as np

#define arrays
x = np.array([1, 2, 3, 4, 5])
y = np.array([12, 14, 14, 19, 22])

#multiply two arrays together
combo = (x)*(y)

#view result
print(combo)

[ 12 28 42 76 110]

Обратите внимание, что на этот раз мы не получили никакой ошибки.

Сценарий 2: отказ от использования функции NumPy Min

Предположим, мы используем следующий код, чтобы попытаться найти минимальное значение массива NumPy:

import numpy as np

#define array of data
data = np.array([3.3, 4.1, 4, 5.6, 8.1, 9.9, 9.7, 10.2])

#attempt to find minimum value of array
min_val = min (data)

#view minimum value
print(min_val)

TypeError : 'numpy.float64' object is not callable 

Мы получаем TypeError , потому что использовали функцию min() .

Вместо этого нам нужно использовать np.min() следующим образом:

import numpy as np

#define array of data
data = np.array([3.3, 4.1, 4, 5.6, 8.1, 9.9, 9.7, 10.2])

#attempt to find minimum value of array
min_val = np.min (data)

#view minimum value
print(min_val)

3.3

Обратите внимание, что на этот раз мы не получили никакой ошибки.

Дополнительные ресурсы

В следующих руководствах объясняется, как исправить другие распространенные ошибки в Python:

Как исправить: столбцы перекрываются, но суффикс не указан
Как исправить: объект «numpy.ndarray» не имеет атрибута «добавлять»
Как исправить: при использовании всех скалярных значений необходимо передать индекс
Как исправить: ValueError: невозможно преобразовать число с плавающей запятой NaN в целое число


One error you may encounter when using Python is:

TypeError: 'numpy.float64' object is not callable

This error may occur in two different scenarios:

  • Scenario 1: Multiplication Without Using * Sign
  • Scenario 2: Failure to Use NumPy Min Function

The following examples shows how to fix this error in each scenario.

Scenario 1: Multiplication Without Using * Sign

Suppose we attempt to multiply two NumPy arrays without using a multiplication sign (*) as follows:

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
combo = (x)(y)

#view result
print(combo)

TypeError: 'numpy.float64' object is not callable 

We receive a TypeError because we didn’t use the multiplication sign (*) when attempting to multiply the two arrays.

The way to avoid this error is to make sure we used the multiplication sign:

import numpy as np

#define arrays
x = np.array([1, 2, 3, 4, 5])
y = np.array([12, 14, 14, 19, 22])

#multiply two arrays together
combo = (x)*(y)

#view result
print(combo)

[ 12  28  42  76 110]

Notice that we receive no error this time.

Scenario 2: Failure to Use NumPy Min Function

Suppose we use the following code to attempt to find the minimum value of a NumPy array:

import numpy as np

#define array of data
data = np.array([3.3, 4.1, 4, 5.6, 8.1, 9.9, 9.7, 10.2])

#attempt to find minimum value of array
min_val = min(data)

#view minimum value
print(min_val)

TypeError: 'numpy.float64' object is not callable 

We receive a TypeError because we used the min() function.

Instead, we need to use np.min() as follows:

import numpy as np

#define array of data
data = np.array([3.3, 4.1, 4, 5.6, 8.1, 9.9, 9.7, 10.2])

#attempt to find minimum value of array
min_val = np.min(data)

#view minimum value
print(min_val)

3.3

Notice that we receive no error this time.

Additional Resources

The following tutorials explain how to fix other common errors in Python:

How to Fix: columns overlap but no suffix specified
How to Fix: ‘numpy.ndarray’ object has no attribute ‘append’
How to Fix: if using all scalar values, you must pass an index
How to Fix: ValueError: cannot convert float NaN to integer

This article explains how to solve the error message typeerror: numpy.float64 object is not callable. This error occurs only in one situation.

Why does the typeerror: ‘numpy.float64’ object is not callable occur?

The error ‘numpy.float64’ object is not callable typically occurs because you are trying to call a NumPy float64 object as if it were a function.

Here’s an example on how the error occur:

import numpy as np

x = np.float64(3.14)
y = x(2)

In this example, x is initialized as a numpy.float64 object with the value 3.14.

Next, the line attempts to call x as if it were a function with the argument 2, which is not valid.

If we run the example code, the output will be raise an error:

Traceback (most recent call last):
File “C:UsersDellPycharmProjectspythonProjectmain.py”, line 4, in
y = x(2) # trying to call x as if it were a function with argument 2
TypeError: ‘numpy.float64’ object is not callable

Common causes of numpy float64 object is not callable

There are multiple common causes of “numpy.float64 object is not callable” error, as follows::

  • You’re using parentheses instead of brackets to access elements of a NumPy array.
  • You are using parentheses instead of brackets to perform mathematical operations on float64 objects.
  • Accidentally overwriting a function with a float64 object of the same name.

How to fix the error ‘numpy.float64’ object is not callable?

Here are the solutions you need to follow to solve the error ‘numpy.float64’ object is not callable:

Solution 1: Remove Parenthesis

For example:

import numpy as np

x = np.float64(3.14)
y = x(2.0)

The second line tries to call x as if it were a function with argument 2.0. This will result in “numpy.float64 object is not callable” error.

To solve this error, you need to remove the parenthesis and modify the second line of your code.

We will use a mathematical operation that is valid for float64 objects, such as addition, subtraction, multiplication, or division.

For example, if you want to multiply x by 2, you can use the following code instead:

import numpy as np

x = np.float64(3.14)
y = x * 2.0

In this example, we remove the parenthesis an we change it to multiplication; operator * is used instead of calling x as a function, which will correctly calculate y as 6.28.

Output:

6.28

Solution 2: Use Brackets Instead of Parentheses

The second step to solve this error is using brackets instead of parenthesis when indexing or slicing numpy arrays.

This is because parentheses are used to call functions, whereas brackets are used for indexing and slicing.

Let’s take a look at the example, instead of this code:

import numpy as np

x = np.array([1, 2, 3])
y = np.float64(5)

result = y(x[0])

Output:

Traceback (most recent call last):
File “C:UsersDellPycharmProjectspythonProjectmain.py”, line 6, in
result = y(x[0])
TypeError: ‘numpy.float64’ object is not callable

You can write this code:

import numpy as np

a = np.array([1, 2, 3])
b = np.float64(5)

result = b

print(result)

Output:

5.0

Solution 3: Check for Overwriting of Functions

Another way to solve this error is to check if you have accidentally overwritten a function with a float64 object of the same name.

This will happen if you are assigning a float64 object to a variable that has the same name as a function that you want to use later.

For example: let’s say you have a function example that you want to use in your code, but you accidentally assign a numpy.float64 object to the variable example:

import numpy as np

def example(x):
    return x + 1

example = np.float64(5)

result = example(3)

Then the expected output will raise an error:

Traceback (most recent call last):
File “C:UsersDellPycharmProjectspythonProjectmain.py”, line 8, in
result = example(3)
TypeError: ‘numpy.float64’ object is not callable

Instead you can write this code to solve the error:

import numpy as np

def example(x):
    return x + 1

example_solve = np.float64(5)

result = example(3)
print(result)

The code defines a function called example that takes a single argument and returns the argument plus one.

Then, it will creates a NumPy float64 scalar called example_solve with value 5.0.

Next, it calls the example function with argument 3 and assigns the result to a variable called result.

Finally, it prints the value of result to the console, which is:

4

Additional Resources

The following tutorials discuss on how you can solve other common errors while using the NumPy library:

  • typeerror: numpy.float64 object cannot be interpreted as an integer
  • attributeerror: module ‘numpy’ has no attribute ‘long’
  • attributeerror: module numpy has no attribute arrange
  • modulenotfounderror: no module named numpy
  • typeerror: numpy.float64 object cannot be interpreted as an integer

Conclusion

In conclusion, we’ve discussed why this error occur, and the common causes of the error.

Also, we provide the solutions and examples to solved the error.

FAQs

What is typeerror numpy.float64 object is not callable means?

The Typeerror numpy.float64 object is not callable means that you are using parentheses to call a float64 object instead of using brackets to access its elements.

What should I do if I accidentally overwrite a function with a float64 object of the same name?

To solve this error, you can either rename the variable or the function so that they have various names.

You can use the del statement to delete the variable and free up the name for use with the function.

How can I prevent the “TypeError: numpy.float64 object is not callable” error from happening in the future?

To prevent this error, make sure to always use brackets to access elements of a NumPy array and to perform mathematical operations on float64 objects.

Improve Article

Save Article

Like Article

  • Read
  • Discuss
  • Improve Article

    Save Article

    Like Article

    In this article, we are going to see how to fix TypeError: ‘numpy.float’ object is not callable in Python. There is only one case in which we can see this error: 

    If we try to call a NumPy array as a function, we are most likely to get such an error. 

    Example:

    Python3

    import numpy as np

    a = np.array([1,2,3])

    a()

    Output:

    TypeError: 'numpy.ndarray' object is not callable

    In the older version of Numpy, we used to see “numpy.float64” instead of “numpy.ndarray”.

    Solution: 

    This can be solved simply by removing the parenthesis after the array. 

    Python3

    import numpy as np

    a = np.array([1,2,3])

    a

    Output:

    array([1, 2, 3])

    Here version of NumPy is ‘1.21.2’.

    Note: In the earlier version of Numpy, we also used to get this error while using Python min() or max() function with a NumPy array. In the recent versions of NumPy, this is solved.  In the earlier versions, this particular error was supposed to be solved using np.max() or np.min() instead of min() and max(). 

    Last Updated :
    28 Nov, 2021

    Like Article

    Save Article

  • Ошибка nullreferenceexception как исправить
  • Ошибка nullreferenceexception object reference not set to an instance of an object
  • Ошибка nullpointerexception java что это
  • Ошибка null при обновлении iphone 6s что означает
  • Ошибка null при обновлении ipad