• Register
1 vote
1.5k 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,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 163 views
163 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 11 views
11 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 9.1k points
0 votes
1 answer 33 views
33 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 15 views
15 views
Problem: At the bottom of the code you have to add these lines: simFlips(100,100) show() Here is the error that I get when I run it on ubuntu: Traceback (most recent call last): File "coin.py", line 36, in <module> simFlips(100,100) File "coin.py", ... (heads - tails)) AttributeError: 'numpy.ndarray' object has no attribute 'append' Please tell me what I'm doing wrong that gives me the last error
asked Jan 11 Mashhoodch 9.1k points
0 votes
1 answer 430 views
430 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, 2020 psandprop 2.4k points
0 votes
1 answer 743 views
743 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, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 5 views
5 views
Problem: I've programmed these for calculating Variance import pandas as pd import xlrd import numpy as np import matplotlib.pyplot as plt credit_card=pd.read_csv("default_of_credit_card_clients_Data.csv",skiprows=1) print(credit_card.head()) for col in credit_card: var[col]=np.var(credit_card(col)) print(var)
asked 2 days ago Mashhoodch 9.1k points
0 votes
1 answer 6 views
6 views
Problem: I am novice to Python and following a tutorial. There is an example of list in the tutorial : example = list('easyhoss') Now, In tutorial, example= ['e','a',...,'s']. But in my case I am getting following error: >>> example = list('easyhoss') ... , line 1, in <module> TypeError: 'list' object is not callable Please tell me where I am wrong. I searched SO this but it is different.
asked Feb 18 Mashhoodch 9.1k points
0 votes
1 answer 17 views
17 views asked Jan 26 khanboy 2.1k points
0 votes
1 answer 13 views
13 views
Problem: The Python &ldquo;typeerror: 'list' object is not callable&rdquo; error is raised when I try to access a list as if it were a function. To solve this error, make sure square brackets are used to access or change values in a list rather than curly brackets.
asked Jan 24 Mashhoodch 9.1k points