Programmers learn & share
+1 vote
22 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)

 

by (12.9k points)   | 22 views

2 Answers

+1 vote

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.

by (55.5k 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.

by (10k points)  
2,221 questions
2,695 answers
59 comments
241 users