Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why numpy.ndarray is object is not callable in my simple for python loop

Tags:

I loaded a text file containing a two column matrix (e.g. below)

[ 1   3   2   4   3   5    2   0] 

My calculation is just to sum each row i.e. 1+3, 2+4, 3+5 and 2+0. I am using the below code:

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

But I received an error saying numpy.ndarray object is not callable. Why does this happen? How can I do this simple calculation? Thanks.

like image 920
mee mee Avatar asked May 14 '16 05:05

mee mee


People also ask

What is Ndarray object in NumPy?

The ndarray object consists of contiguous one-dimensional segment of computer memory, combined with an indexing scheme that maps each item to a location in the memory block. The memory block holds the elements in a row-major order (C style) or a column-major order (FORTRAN or MatLab style).

Can NumPy Ndarray hold any type of object?

NumPy arrays are typed arrays of fixed size. Python lists are heterogeneous and thus elements of a list may contain any object type, while NumPy arrays are homogenous and can contain object of only one type.

Is Ndarray same as NumPy array?

numpy. array is just a convenience function to create an ndarray ; it is not a class itself. You can also create an array using numpy. ndarray , but it is not the recommended way.

Is NumPy Ndarray a sequence?

A numpy array is a sequence, but it is not a Sequence as it is not registered as a subclass of Sequence.


1 Answers

The error TypeError: 'numpy.ndarray' object is not callable means that you tried to call a numpy array as a function.

Use

Z=XY[0]+XY[1] 

Instead of

Z=XY(i,0)+XY(i,1) 
like image 65
Vadiraja K Avatar answered Sep 19 '22 00:09

Vadiraja K