Home Python cluster module confusing class methods
Reply: 0

Python cluster module confusing class methods

data_starting
1#
data_starting Published in 2017-12-06 20:03:07Z

I was trying to write a module for clustering, but class method are a little confusing for me

import numpy as np

class SingleLinkage:

    def __init__(self, x, k):
        self.data = x
        n=x.shape[0]
        self.clusters={i : [i] for i in range(n)}
        self.k=k
        self.y=None

    def euc(self,x,y):
        self.x=x
        if self.y==None:
            self.y=y 
        return np.sqrt(np.sum((x-y)**2))

    def clustDist(self, i, j):
        cd=np.Inf
        for idx in self.clusters[i]:
            for idy in self.clusters[j]:
                cd=min(cd,self.euc(self.data[idx], self.data[idy]))
        return cd

    def findClothest(self):
        cd=np.Inf
        for i in self.clusters:
            for j in self.clusters:
                if i!=j and clustDist(i,j)<cd:
                    cc=(i,j)
                    cd=clustDist(i,j)
    def merge(self,i,j):
        self.clusters[i]+=self.clusters[j]
        self.clusters.pop(j)

When I run it, it gives me:

>>> x = np.array ([[0 , 0] , [0 , 0.1] , [1 , 1]])
>>> hc = lc.SingleLinkage (x , 2)
>>> hc.clusters
{0: [0], 1: [1], 2: [2]}

But should be different:

>>> hc.clusters
{0: [0 , 1] , 1: [2]}

I know that probably the error is in the init function. How should I change it?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.329103 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO