• Register
1 vote
786 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

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,240 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
2 answers 38 views
38 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 psandprop 2.4k points
0 votes
1 answer 12 views
12 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 psandprop 2.4k points
0 votes
1 answer 56 views
56 views
When trying to find the index of v but it gave me: 'numpy.ndarray' object has no attribute 'index' none of them have answered my question v = np.random.randn(10) print(v) maximum = np.max(v) minimum = np.min(v) print(maximum, minimum) v.index(maximum, minimum)
asked Oct 30 psandprop 2.4k points
0 votes
1 answer 399 views
399 views
Problem: Recently I have joined new project on python and numpy. My below code is returning me below error: Attributeerror: 'numpy.ndarray' object has no attribute 'append' . Please find my code below for the reference: class1 = np.array([]) #It creates 2 ... to print my results in the scatter graphic with the two colors where I can try to identify the visually the class1 and class2 elements.
asked Jun 17 Raphael Pacheco 4.9k points
1 vote
1 answer 20 views
20 views
I tried to use is_authenticated() in a view, but got the error `TypeError: 'bool' object is not callable. How do I fix it? This is my code def func_name(): if current_user.is_authenticated() \ and not current_user.confirmed \ and request.endpoint[:5] != ' ... ('auth.unconfirmed')) In some cases a class might implement __call__ function which you can call an object too, then it will be callable.
asked Nov 8 kevin muturi 220 points