‘numpy.ndarray’ object is not callable – Python Error

Total
0
Shares

So you are getting this numpy python error ‘numpy.ndarray’ object is not callable. Right? This is because somewhere in your code you are using a non-function element like int, array, object, list etc. as function. For example –

Suppose, you have an array – myArray = [2, 3, 4] . Now to access the first element, you should use myArray[0]. But sometimes we mistakenly use myArray(0).

import numpy as np
data = np.array([[1, 2], [3, 4]])
xy = data
for XY in xy:
   i=0  
   Z=XY(i,0)+XY(i,1)
   i=i+1      
   print (Z)

This will throw error, ‘numpy.ndarray‘ object is not callable, because XY is the vector not function. And we are fetching the value, not by index but by passing parameter to it. So, XY(i, 0) is the wrong way. The correct way is XY[0] and XY[1].

Now, let’s correct our above code snippet –

import numpy as np
data = np.array([[1, 2], [3, 4]])
xy = data
for XY in xy:
   i=0  
   Z=XY[0]+XY[1]
   i=i+1      
   print (Z)

The major reason for this error is the conflict of user defined variable names with system functions. For example, you define an integer variable round and try to use math function round. Python will throw error, “Object is not callable”

    Tweet this to help others

Sign Up for Our Newsletters

Get only the best articles delivered to your mail

You May Also Like
python run bash script in background

python run bash script in background

Table of Contents Hide IntroductionTerminating background script after a timeoutHow to check if process is still running or terminated?How to get output from process?ConclusionLive DemoRelated Posts: Python subprocess library is…
View Post

modulenotfounderror: no module named ‘numpy’

Table of Contents Hide Installing numpyCheck Environment PATHReinstall Python and NumpyVirtual EnvironmentsRelated Posts: Python throws modulenotfounderror: no module named ‘numpy’, in four conditions – When you have multiple versions of…
View Post

Convert list to string in Python

Table of Contents Hide Joining with delimiterJoining integer itemsUsing map() functionUsing lambda functionUsing LoopsLive DemoRelated Posts: Python provides join() function which could be used to convert list to string. If…
View Post