Like Python
lists
and
arrays
, we can use indexing with NumPy arrays to access individual elements from them. In indexing, we use the index value of the element inside the square bracket
[]
preceded by the array name and retrieve the element. But if we use parenthesis
()
instead of square brackets, Python thinks of the variable as a function and tries to call it, but there would be no function with that name, so it would return the
‘numpy.ndarray’ object is not callable
Error.
In this Python tutorial, we will have a look at this Python error and see why this error occurs in Python and how to debug it. We will also discuss the error with the help of an example, so you can get a better idea about the error.
So without further ado, let's get started with the Error.
Python Error: TypeError: 'numpy.ndarray' object is not callable
Python numpy , is a third-party scientific computational library that is mostly used for its popular and powerful array data structure. The Python NumPy's array is a faster and more math-centric data structure as compared to the Python list. And similar to the Python list, we can use indexing with Numpy Array to access an individual element.
But if we use the parenthesis
()
instead of a square bracket
()
while retrieving the element from a numpy array, we get the following error.
TypeError: 'numpy.ndarray' object is not callable
The above error statement has two parts
Error Type
and
Error Message
.
-
Error Type (
TypeError
): It is a type of Python exception that occurs when we perform an invalid or incorrect operation on a Python data type object . -
Error Message (
'numpy.ndarray' object is not callable
): This error message is telling us that we are trying to call a numpy array object as a function that is invalid in Python.
Example
When we write the parenthesis
()
after a variable name, Python treats it as a function call, and this same goes when we use the parenthesis after the numpy array object by mistake.
Let's create a numpy array and try to access the first element of the array using its index value
0
. But here, rather than using a square bracket, we will use the parenthesis to retrieve the element and see what we get as an output.
import numpy as np
# create an array using numpy array
arr = np.array([10, 20, 30, 40, 50])
# print first element of the array using parenthesis
print(arr(0))
Output
Traceback (most recent call last):
File "main.py", line 7, in <module>
print(arr(0))
TypeError: 'numpy.ndarray' object is not callable
Break the code
In the above program, we got this error at line 7, where we are trying to access the first element of the numpy array using parenthesis. But Python treats the
arr(0)
statement as a function call and threw the error because
arr
is not a function but a numpy array object.
Solution
The solution of the above program is very simple. All we need to do is replace the
()
parenthesis with the square bracket
[]
while accessing the numpy array element.
Solution Example
import numpy as np
def arr(a):
print(a)
# create an arra using numpy array
arr = np.array([10, 20, 30, 40, 50])
# print first element of the array using parenthesis
print(arr[0])
Output
10
Wrapping Up!
In this Python tutorial, we learned about Python Numpy Array error
TypeError: 'numpy.ndarray' object is not callable
. This error occurs in Python when we use the parenthesis after the numpy array object instead of the square bracket. To solve this error, all you need to do is use the following syntax
array_name[index]
while accessing the numpy array elements. If you are still getting this error in Python, please share your code in the comment section, we will try to help you in debugging.
People are also reading:
- Python Frameworks
- Read File in Python
- Best Way to Learn Python
- How to Play sounds in Python?
- Python Operators
- What is a constructor in Python?
- How To Make a Game With Python?
- Python Error: TypeError: ‘tuple’ object is not callable Solution
- Enumerate in Python
- Python typeerror: ‘str’ object is not callable Solution
Leave a Comment on this Post