繁体   English   中英

Python TypeError:“ float”对象没有属性“ __getitem__”

[英]Python TypeError: 'float' object has no attribute '__getitem__'

有人知道如何解决这个问题吗? 我现在正在研究集体智慧,当我编写此代码时,我将其与另一个示例进行了比较。 但是它会出现这样的错误:

Traceback (most recent call last):   File "<pyshell#8>", line 1, in
 <module>
    clust=clusters.hcluster(data)   File "D:\Kuliah\smt1\Phyton Class\contoh coding\coding-collective
intelligence\myself_Maulida\bab3-documentClustering\clusters.py", line
78, in hcluster
    for i in range(len(clust[0].vec))] TypeError: 'float' object has no attribute '__getitem__'

这是我的代码,有人可以帮忙吗? 谢谢。

def hcluster(rows,distance=pearson):
    distances={}
    currentclustid=-1

    #clusters are initially just the rows
    clust=[bicluster(rows[i],id=i) for i in range(len(rows))]

    while len(clust)>1:
        lowestpair=(0.1)
        closest=distance(clust[0].vec,clust[1].vec)

        #loop through every pair looking for the smallest distance 
        for i in range(len(clust)):
            for j in range(i+1,len(clust)):
            #distance is the cache of distance calculations
                if(clust[i].id,clust[j].id) not in distances:
                    distances[(clust[i].id,clust[j].id)]=distance(clust[i].vec,clust[j].vec)

                    d=distances[(clust[i].id,clust[j].id)]

                if d<closest:
                    closest=d
                    lowestpair=(i,j)

            #calculate the average of the two cluster
            mergevec=[
            (clust[lowestpair[0]].vec[i]+clust[lowestpair[1]].vec[i])/2.0 
            for i in range(len(clust[0].vec))]

            #create the new cluster
            newcluster=bicluster(mergevec,left=clust[lowestpair[0]],
                                 right=clust[lowestpair[1]],
                                 distance=closest,id=currentclustid)

            #cluster ids that weren't in the original set are negative
            currentclustid-=1
            del clust[lowestpair[1]]
            del clust[lowestpair[0]]
            clust.append(newcluster)

        return clust[0]
lowestpair=(0.1)

那是一个时期,而不是逗号。 lowestpair是一个浮点数,而不是一个元组。 (虽然看起来错误来自clust[0] ,但是Python的回溯并不是很好地指出错误来自逻辑线的哪条物理线。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM