Like any regular Python array, you can access the contents of a NumPy array using indexing. The indexing method, which uses square brackets, lets you see one item in a list or a particular part of a list. If you try to retrieve an item from a NumPy array using round brackets, you will encounter an error.
In this guide, we’re going to talk about what the 'numpy.ndarray' object is not callable
error means. We will then discuss an example scenario of this error in action and show you the solution. Let’s get started.
‘numpy.ndarray’ object is not callable
The 'numpy.ndarray' object is not callable
Python error indicates you are trying to call a NumPy array as if it were a function. This happens if you use round brackets ( ) instead of square brackets [ ] to retrieve items from a list.
The fix to this error is simple: you must replace ( ) with [ ] when you are indexing.
In Python, square brackets indicate indexing whereas round brackets denote a function call. If you use round brackets while trying to access an item in an array, Python cannot handle your code because the array is not structured like a function.
Let’s walk through an example of this error.
An Example Scenario
We are building a program that evaluates the grades of every student in a school. We want to calculate the average grade of each student.
Each student’s grades are stored in a NumPy array like this:
import numpy as np student = np.array([72, 87, 80, 69, 91])
The final item indicates the grade a student received on their final exam. We want to print that grade individually to the console before we show the average grade of each student.
To show the exam grade and average grade for each student, we could use this code:
exam_grade = student(-1) print("This student earned {} on their exam.".format(exam_grade)) average_grade = np.sum(student) / student.size print("This student received an average {} mark on each test.".format(average_grade))
The first line of code finds the last item in our list. This item represents the mark a student received on their final exam. The next line displays the grade the student earned on their exam on the Python console. We then calculate the average grade the student earned and print that value to the console.
Let’s run our code:
Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'numpy.ndarray' object is not callable
Our code returns an error.
The Solution
When we try to access the last item in our student list, we use:
exam_grade = student(-1) print("This student earned {} on their exam.".format(exam_grade))
This line of code causes an error because you cannot use round brackets to access an item from a list. You must use square brackets. To fix our earlier code, we can replace the round brackets with square brackets:
exam_grade = student[-1] print("This student earned {} on their exam.".format(exam_grade))
Our code now returns:
This student earned 91 on their exam. This student received an average 79.8 mark on each test.
Our program retrieves the grade the student earned on their final exam. Then our program calculates the average grade a student received. Our program works as intended.
Conclusion
The Python 'numpy.ndarray' object is not callable
error is caused by using round brackets instead of square brackets to access an item from a NumPy array. To fix this error, use array_name[index_number] syntax to access an item from an array.
Do you want to learn more about coding in Python? Read our How to Learn Python guide. This guide contains expert learning advice to help you expand your knowledge of the Python programming language.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.