TypeError:'numpy.ndarray'对象在我的代码中不可调用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TypeError:'numpy.ndarray'对象在我的代码中不可调用相关的知识,希望对你有一定的参考价值。

我写了两个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)

这会影响内置函数sum,你试着在这里调用它:

    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

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

以上是关于TypeError:'numpy.ndarray'对象在我的代码中不可调用的主要内容,如果未能解决你的问题,请参考以下文章

TypeError: 'QueryDict' object is not callable

Python: TypeError: 'dict' object is not callable

Python: TypeError: 'dict' object is not callable

python: "TypeError: 'type' object is not subscriptable"

Python_报错:TypeError: file must have 'read' and 'readline' attributes

2. python提示:TypeError: unhashable type: 'list'