. Advertisement .
..3..
. Advertisement .
..4..
Most of the web applications are designed with Python, and NumPy is its library that comprises multidimensional arrays and routines for those array’s processing. NumPy is an acronym for Numerical Python. Working with NumPy, different errors can occur. Today, we discuss TypeError: ‘numpy.ndarray’ object is not callable.
Let’s focus on this error and figure out the cause of errors with scenarios and solutions
How To Fix TypeError: ‘numpy.ndarray’ object is not callable
While working with NumPy, this error is quite disturbing;
TypeError: 'numpy.ndarray' object is not callable
The most obvious cause of this error is the use of brackets. Using round brackets ()
instead of square brackets []
. To fix the error, you just need to replace the round brackets with square brackets when indexing.
Checkout the examples scenario with solutions
Scenario 1: Use the round brackets
Let’s discuss a program to find the grades of every student and calculate the average grade of each student in a school
import numpy as np
student = np.array([88, 92, 95, 78, 80, 95])
To return the exam and average grade of each student
examGrade = student(-3)
print("This student earned {} on their exam.".format(examGrade))
averageGrade = np.sum(student) / student.size
print("This student received an average {} mark on each test.".format(averageGrade))
Error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'numpy.ndarray' object is not callable
Solution
For trying to get the last item on the student list,
examGrade = student(-3)
print("This student earned {} on their exam.".format(examGrade))
This code causes the error because of the round brackets, let’s replace it with square brackets
examGrade = student[-3]
print("This student earned {} on their exam.".format(examGrade ))
Output
This student earned 78 on their exam.
This student received an average 88.0 mark on each test.
Scenario 2: Use NumPy array as a function
Another reason of occurring this typreerror is to call an array as a function
import numpy as np
arr = np.array([0, 5, 10, 15, 20, 25])
arr()
Error message:
TypeError: 'numpy.ndarray' object is not callable
The cause of this error is treating the NumPy array as a function. Removing the function sigh can solve the error
Solution
import numpy as np
arr = np.array([0, 5, 10, 15, 20, 25])
print(arr)
Output
[0, 5, 10, 15, 20, 25]
Scenario 3: forgot to add the Multiplication sign (*)
While multiplying two NumPy arrays, you can see the error if you forgot to add the Multiplication sign (*). See the example
import numpy as np
arr1 = np.array([2, 4, 6, 8, 10, 12])
arr2 = np.array([3, 6, 9, 12, 15, 18])
result = (arr1)(arr2)
print(result)
Error message:
TypeError: 'numpy.ndarray' object is not callable
Solution
import numpy as np
arr1 = np.array([2, 4, 6, 8, 10, 12])
arr2 = np.array([3, 6, 9, 12, 15, 18])
result = (arr1) * (arr2)
print(result)
Output
[ 6 24 54 96 150 216]
Conclusion
And here, we discussed the error “TypeError: ‘numpy.ndarray’ object is not callable“. The error causes and how to fix it also highlighted. I hope with different scenarios and solutions, you can design your program quickly and efficiently.
Leave a comment