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

前端 未结 4 921
我寻月下人不归
我寻月下人不归 2020-12-08 22:41

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

相关标签:
4条回答
  • 2020-12-08 23:05

    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)
    
    0 讨论(0)
  • 2020-12-08 23:16

    Avoid loops. What you want to do is:

    import numpy as np
    data=np.loadtxt(fname="data.txt")## to load the above two column
    print data
    print data.sum(axis=1)
    
    0 讨论(0)
  • 2020-12-08 23:17

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

    Numpy Indexing

    Also try and avoid .txt files if you are dealing with matrices. Try to use .csv or .npy files, and use Pandas dataframework to load them just for clarity.

    0 讨论(0)
  • 2020-12-08 23:19

    Sometimes, when a function name and a variable name to which the return of the function is stored are same, the error is shown. Just happened to me.

    0 讨论(0)
提交回复
热议问题