Hello coders!! In this article, we will be learning how to fix the error ‘NumPy.ndarray object is not callable’ and will also look at what exactly causes this error. We know that NumPy is an inbuilt Python module used for array manipulations. However, a small mistake here and there is quite natural. Let us now look into such errors and learn how to resolve them.
Cause of Error:
This error numpy.ndarray object is not callable occurs when one tries to use NumPy as a function. Let us see some common mistakes that might lead to this error.
1) Error During Creation of Simple Array
arr = np.array([[1,2,3],[4,5,6],[7,8,9]])()
arr
OUTPUT:
In this example, we have used () after the np.array() method which gives rise to ‘NumPy.ndarray object is not callable’ error.
2) NumPy.ndarray object is not callable error while using read_csv:
df = pd.read_csv("cars.csv")
mpg=df["mpg"].values()
mpg
OUTPUT:
This mistake is quite common It is done while using the pandas read_csv() method. In this example, we have a CSV data and we want to retrieve the values of some specific column. So, we have used that column name inside the square bracket []. But it may happen that we use values() instead of values. This leads to the error.
Resolution for ‘NumPy.ndarray object is not callable’ error:
Now that we know what causes the error, let us learn how to rectify such mistakes.
1) Error During Creation of Simple Array
For the first instance, while creating an array using the np.array() method, we must not include an extra set of parentheses () at the end of the function to avoid the error.
arr = np.array([[1,2,3],[4,5,6],[7,8,9]])
arr
OUTPUT:
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
2) NumPy.ndarray object is not callable error while using read_csv:
The error was caused because we used values() instead of values. As it is not a function and yet we tried to call it, it gave us an error. So, in order to avoid this error, we must not use values as method.
df = pd.read_csv("cars.csv")
mpg=df["mpg"].values()
mpg
OUTPUT:
More to Read >> Demystifying Python Attribute Error With Examples
Conclusion:
These are the few mistakes that can cause an error. To avoid it in future, one must ensure to not call NumPy as function.
You used at problem 2 the same picture again. That’s really confusing
No, they’re different if you look closely.
don’t know it looks the same to me. i’m confused too