类型错误:"str"对象在循环中调用函数时不可调用


def gain(info,decision_class,unique_info,unique_class):
gain=info[0]
info.append(decision_class)
del info[0],decision_class[0],unique_info[0],unique_class[0]
count=[]
count_yes_no=[]
temp=0
temp1=0
for i in range(len(unique_info)):
    temp=0
    temp1=0
    for j in range(len(info)):
        if(unique_info[i]==info[j]):
            temp=temp+1
    count.append(temp)

for i in range(len(unique_info)):
#print unique_info[i]
    for k in range(len(unique_class)):
    #print unique_class[k]
        temp1=0
        for l in range(len(info)-1):
            #print info[l]
            if(unique_info[i]==info[l]):
                #print str(unique_info[i])+"=="+str(info[l])
                if(unique_class[k]==info[14][l]):
                    #print unique_class[k]unique_info[i]
                    temp1=temp1+1
        count_yes_no.append(temp1)

d=sum(count)
pointer=0
final=[]
for i in range(len(count)):
    x=(count[i]/d)
    y=(count_yes_no[pointer]/count[i])
    z=(count_yes_no[pointer+1]/count[i])
    if(y!=0):
        l1=math.log(y,2)
    else:
        l1=0
    if(z!=0):
        l2=math.log(z,2)
    else:
        l2=0
    f=x*(-y*l1-z*l2)
    final.append(f)
    #print x,y,z,f
    pointer = int(pointer+len(count_yes_no)/len(count))
#print "nInfo("+gain+")    n"+str(sum(final))
return sum(final),gain
#print gain,info, decision_class, unique_info ,count  ,unique_class
#print info
#print count,count_yes_no,d 
data=get_data()    
info,unique_info,counts=get_info(data)    
entropy=entropy(unique_info,counts)   
for i in range(len(info)-1):    
           info_gain,gain=gain(info[i],info[len(info)-1],unique_info[i],unique_info[len(unique_info)-1])   
    print "Info("+gain+") ",info_gain  

显示的错误是


0.940285958671信息(年龄) 0.693536138896

Traceback (most recent call last):
  File "G:test.py", line 113, in <module>
    info_gain,gain=gain(info[i],info[len(info)-1],unique_info[i],unique_info[len(unique_info)-1])
TypeError: 'str' object is not callable
>>> 

您使用gain作为函数和变量的名称:

info_gain, gain = gain(...
                  ^^^^ function
           ^^^^ variable

为它们指定不同的名称,例如将函数重命名为 compute_gain()

相关内容

  • 没有找到相关文章

最新更新