0

Anybody knows how to solve this problem? I'm studying collective intelligence now, and I compared with another example when I made this code. But It's getting error like this:

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__'

Here is my code, anybody can help? Thank you.

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]
0

1 Answer 1

1
lowestpair=(0.1)

That's a period, not a comma. lowestpair is a float, rather than a tuple. (While it looks like the error comes from clust[0], Python tracebacks aren't very good at pointing out which physical line of a logical line an error comes from.)

Sign up to request clarification or add additional context in comments.

1 Comment

I got it, it should be lowestpair=(0,1) not lowestpair=(0.1). Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.