• Register
1 vote
1.8k views

Problem:

I have an array with some data in a text file. I want to load it to my program and run an adding function in my program. I got this error “numpy.ndarray object is not callable” When I run my program.
Any solution? Array and sample codes attached below.

[ 1   3
  2   4
  3   5 
  2   0]
data=np.loadtxt(fname="textfile.txt")## to load the above two column
xy= data
for XY in xy:
   n=0  
   Z=XY(n,0)+XY(n,1)
   n=i+1      
   print (Z)

 

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

2 votes

Solution:

numpy.ndarray object is not callable happened beacuse you called numpy array as a function.

You had to use

Z=XY[0]+XY[1]

instead of

Z=XY(n,0)+XY(n,1)

I hope you’ve got your answer. Thanks.

13 9 6
94,260 points
0 votes

Solution:

The error TypeError: 'numpy.ndarray' object is not callable means that you tried to call a numpy array as a function. We can reproduce the error like so in the repl:

In [16]: import numpy as np

In [17]: np.array([1,2,3])()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/user/<ipython-input-17-1abf8f3c8162> in <module>()
----> 1 np.array([1,2,3])()

TypeError: 'numpy.ndarray' object is not callable

EXplanation:

If we are to assume that the error is indeed coming from the snippet of code that you posted (something that you should check,) then you must have reassigned either pd.rolling_mean or pd.rolling_std to a numpy array earlier in your code.

We are actually mean

In [1]: import numpy as np

In [2]: import pandas as pd

In [3]: pd.rolling_mean(np.array([1,2,3]), 20, min_periods=5) # Works
Out[3]: array([ nan,  nan,  nan])

In [4]: pd.rolling_mean = np.array([1,2,3])

In [5]: pd.rolling_mean(np.array([1,2,3]), 20, min_periods=5) # Doesn't work anymore...
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/user/<ipython-input-5-f528129299b9> in <module>()
----> 1 pd.rolling_mean(np.array([1,2,3]), 20, min_periods=5) # Doesn't work anymore...

TypeError: 'numpy.ndarray' object is not callable

So, basically you need to search the rest of your codebase for pd.rolling_mean = ... and/or pd.rolling_std = ... to see where you may have overwritten them.

Also, if you'd like, you can put in reload(pd) just before your snippet, which should make it run by restoring the value of pd to what you originally imported it as, but I still highly recommend that you try to find where you may have reassigned the given functions.

 

Avoid Loops

import numpy as np
data=np.loadtxt(fname="data.txt")## to load the above two column
print data
print data.sum(axis=1)

Avoid the for loopfor XY in xy: Instead read up how the numpy arrays are indexed and handled.

The definition of fmin according to scipy is:

fmin(func, x0, args=(), **kwargs)

The reason fmin doesn't take Rs is because it isn't a callable, but an array. A callable is simply an object that implements __call__, such objects are methods, anonymous functions(lambdas), classes and instantiated classes.

Follw this below code

def callable_function(*vargs):
    pass

class Callable:
    def __init__(self, *vargs):
        pass
    def __call__(self, *vargs):
        pass
    @staticmethod
    def staticcallable(*vargs):
        pass

callable_lambda = lambda *vargs: None

All of the above are callables. However, not all callables can be used with fmin as it expects the callable to return an int or a float.

10 6 4
31,120 points

Related questions

0 votes
1 answer 26 views
26 views
Problem: Any solution: numpy.ndarray' object is not callable .
asked Mar 14 Wafa Abu Yousef 6.1k points
0 votes
1 answer 24 views
24 views
Problem: I received an error saying numpy.ndarray object is not callable. Why does this happen? Thanks
asked Mar 16 ummesalma 29.2k points
0 votes
1 answer 13 views
13 views
Problem: I am gitting this error: How to solve python numpy ValueError: operands could not be broadcast together with shapes
asked Mar 19 Wafa Abu Yousef 6.1k points
0 votes
2 answers 493 views
493 views
In numpy, I have two "arrays", X is (m,n) and y is a vector (n,1) using X*y I am getting the error ValueError: operands could not be broadcast together with shapes (97,2) (2,1) When (97,2)x(2,1) is clearly a legal matrix operation and should give me a (97,1) vector EDIT: I have corrected this using X.dot(y) but the original question still remains.
asked Oct 24, 2020 psandprop 2.4k points
0 votes
1 answer 110 views
110 views
Problem: I am trying to evaluate a formula, np is numpy: Ds = pow(10,5) D = np.linspace(0, pow(10,6), 100) alpha=1.44 beta=0.44 A=alpha*(D/Ds) L=1.65 buf2=L/4.343 buf=pow(-(alpha*[D/Ds]),beta) value=exp(buf) and then I will plot this data but I get: buf=pow(-(alpha*[D/Ds]),beta) TypeError: can't multiply sequence by non-int of type 'float' How can I overcome this?
asked Feb 17 Mashhoodch 13k points
0 votes
1 answer 59 views
59 views
I know there is a method for a Python list to return the first index of something: >>> l = [1, 2, 3] >>> l.index(2) 1 Is there something like that for NumPy arrays?
asked Oct 26, 2020 psandprop 2.4k points
0 votes
1 answer 10 views
10 views
Problem: Could you tell me what I am doing wrong and what I need to do to fix it? Thanks in advance.
asked May 3 salim1 60.6k points
0 votes
1 answer 24 views
24 views
Problem: I'm trying to delete the first 24 rows of my pandas dataframe. Searching on the web has led me to believe that the best way to do this is by using the pandas 'drop' function. However, whenever I try to use it, I get the error: AttributeError: 'numpy. ... should do to delete the first 24 rows of my dataframe. (This was all done using Python 3 on a Jupyter notebook on my local machine)
asked May 3 muktaa 34.6k points
0 votes
1 answer 4 views
4 views
Problem: I upgraded numpy to the latest version and now I am getting following error on importing numpy: AttributeError: type object 'numpy.ndarray' has no attribute 'array_function' I am using numpy version 1.16.
asked Apr 26 Supti 13k points
0 votes
1 answer 4 views
4 views
Problem: any ideas on what i need to do for this problem? attributeerror: type object 'numpy.ndarray' has no attribute '__array_function__'
asked Apr 10 Rohit kr 17.5k points