Python 类型错误:';numpy.ndarray和#x27;对象在我的代码中不可调用

Python 类型错误:';numpy.ndarray和#x27;对象在我的代码中不可调用,python,python-3.x,numpy,error-handling,Python,Python 3.x,Numpy,Error Handling,我已经编写了两个python代码。代码1显示“numpy.ndarray”对象不可调用的错误。但当我注释代码2中的某个部分时,程序运行成功。我不熟悉这种语言,所以我不知道问题出在哪里。有语法错误吗?谁能解决这个问题 代码1: import numpy as np import csv x = np.genfromtxt("boston_housing.csv",dtype=float,delimiter=',',skip_header=1,usecols = (0,1,2,3,4,5,6,7,

我已经编写了两个python代码。代码1显示“numpy.ndarray”对象不可调用的错误。但当我注释代码2中的某个部分时,程序运行成功。我不熟悉这种语言,所以我不知道问题出在哪里。有语法错误吗?谁能解决这个问题

代码1:

import numpy as np
import csv

x = np.genfromtxt("boston_housing.csv",dtype=float,delimiter=',',skip_header=1,usecols = (0,1,2,3,4,5,6,7,8,9,10,11,12))

xx = x
y = np.genfromtxt("boston_housing.csv",dtype=float,delimiter=',',skip_header=1,usecols = (13))


ave = np.zeros(13)
sum = np.zeros(13)
mn = [x[0][0],x[0][1],x[0][2],x[0][3],x[0][4],x[0][5],x[0][6],x[0][7],x[0][8],x[0][9],x[0][10],x[0][11],x[0][12]]
mx = [x[0][0],x[0][1],x[0][2],x[0][3],x[0][4],x[0][5],x[0][6],x[0][7],x[0][8],x[0][9],x[0][10],x[0][11],x[0][12]]
for row in x:
    for i in range(0,13):
        sum[i] = sum[i] + row[i]
        mn[i] = min(mn[i],row[i])
        mx[i] = max(mx[i],row[i])

alpha = 0.001
theta = [0,0,0,0,0,0,0,0,0,0,0,0,0,0]
for ll in range(0,1):
    temp = []
    grad0 = (1.0/506)*sum([(theta[0] + theta[1]*xx[i][0] + theta[2]*xx[i][1] + theta[3]*xx[i][2] + theta[4]*xx[i][3] + theta[5]*xx[i][4] + theta[6]*xx[i][5]
                        + theta[7]*xx[i][6] + theta[8]*xx[i][7] + theta[9]*xx[i][8] + theta[10]*xx[i][9] + theta[11]*xx[i][10] + theta[12]*xx[i][11]
                        + theta[13]*xx[i][12] - y[i]) for i in range (506)])
    temp.append(theta[0] - (alpha * grad0))
    for j in range(1,14):
        grad0 = (1.0/506)*sum([(theta[0] + theta[1]*xx[i][0] + theta[2]*xx[i][1] + theta[3]*xx[i][2] + theta[4]*xx[i][3] + theta[5]*xx[i][4] + theta[6]*xx[i][5]
                            + theta[7]*xx[i][6] + theta[8]*xx[i][7] + theta[9]*xx[i][8] + theta[10]*xx[i][9] + theta[11]*xx[i][10] + theta[12]*xx[i][11]
                            + theta[13]*xx[i][12] - y[i])*xx[i][j-1] for i in range (506)])
        temp.append(theta[j] - (alpha * grad0))
    theta = temp
yy = [0.02501,35,4.15,1,0.77,8.78,81.3,2.5051,24,666,17,382.8,11.48]
ans = 0
for i in range(0,13):
    ans = ans + (yy[i] * theta[i+1])
ans = ans + theta[0]
print(theta)
print(ans) 
代码2:

import numpy as np
import csv

x = np.genfromtxt("boston_housing.csv",dtype=float,delimiter=',',skip_header=1,usecols = (0,1,2,3,4,5,6,7,8,9,10,11,12))
xx = x
y = np.genfromtxt("boston_housing.csv",dtype=float,delimiter=',',skip_header=1,usecols = (13))

"""
ave = np.zeros(13)
sum = np.zeros(13)
mn = [x[0][0],x[0][1],x[0][2],x[0][3],x[0][4],x[0][5],x[0][6],x[0][7],x[0][8],x[0][9],x[0][10],x[0][11],x[0][12]]
mx = [x[0][0],x[0][1],x[0][2],x[0][3],x[0][4],x[0][5],x[0][6],x[0][7],x[0][8],x[0][9],x[0][10],x[0][11],x[0][12]]
for row in x:
    for i in range(0,13):
        sum[i] = sum[i] + row[i]
        mn[i] = min(mn[i],row[i])
        mx[i] = max(mx[i],row[i])
"""
alpha = 0.001
theta = [0,0,0,0,0,0,0,0,0,0,0,0,0,0]
for ll in range(0,1):
    temp = []
    grad0 = (1.0/506)*sum([(theta[0] + theta[1]*xx[i][0] + theta[2]*xx[i][1] + theta[3]*xx[i][2] + theta[4]*xx[i][3] + theta[5]*xx[i][4] + theta[6]*xx[i][5]
                        + theta[7]*xx[i][6] + theta[8]*xx[i][7] + theta[9]*xx[i][8] + theta[10]*xx[i][9] + theta[11]*xx[i][10] + theta[12]*xx[i][11]
                        + theta[13]*xx[i][12] - y[i]) for i in range (506)])
    temp.append(theta[0] - (alpha * grad0))
    for j in range(1,14):
        grad0 = (1.0/506)*sum([(theta[0] + theta[1]*xx[i][0] + theta[2]*xx[i][1] + theta[3]*xx[i][2] + theta[4]*xx[i][3] + theta[5]*xx[i][4] + theta[6]*xx[i][5]
                            + theta[7]*xx[i][6] + theta[8]*xx[i][7] + theta[9]*xx[i][8] + theta[10]*xx[i][9] + theta[11]*xx[i][10] + theta[12]*xx[i][11]
                            + theta[13]*xx[i][12] - y[i])*xx[i][j-1] for i in range (506)])
        temp.append(theta[j] - (alpha * grad0))
    theta = temp
yy = [0.02501,35,4.15,1,0.77,8.78,81.3,2.5051,24,666,17,382.8,11.48]
ans = 0
for i in range(0,13):
    ans = ans + (yy[i] * theta[i+1])
ans = ans + theta[0]
print(theta)
print(ans)

在第一个脚本的开头有一行:

sum = np.zeros(13)
隐藏了内置函数,您尝试在此处调用该函数:

    grad0 = (1.0/506)*sum([(theta[0] + theta[1]*xx[i][0] + theta[2]*xx[i][1] + theta[3]*xx[i][2] + theta[4]*xx[i][3] + theta[5]*xx[i][4] + theta[6]*xx[i][5]
                    + theta[7]*xx[i][6] + theta[8]*xx[i][7] + theta[9]*xx[i][8] + theta[10]*xx[i][9] + theta[11]*xx[i][10] + theta[12]*xx[i][11]
                    + theta[13]*xx[i][12] - y[i]) for i in range (506)])
您已经将
sum
重新定义为一个numpy数组,现在您正在尝试调用
sum()
。这将生成错误,因为numpy数组不可调用。比如说,

In [7]: x
Out[7]: array([ 1.,  2.,  3.])

In [8]: x(99)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-05cea57e85d3> in <module>()
----> 1 x(99)

TypeError: 'numpy.ndarray' object is not callable
[7]中的
:x
Out[7]:数组([1,2,3.])
In[8]:x(99)
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1 x(99)
TypeError:“numpy.ndarray”对象不可调用

要解决此问题,请将
sum
数组的名称更改为类似于
total

的名称。请提供完整的回溯。此外,您肯定想查看。您的问题被否决,因为它缺少重要信息。您已经包含了您的代码,这很好,但是如果(作为Max Powers请求)包含完整的回溯(即完整的错误消息),则会更好。回溯显示有关错误的重要信息,包括触发错误的行。