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) #returns the indices of the clothest clusters def merge(self,i,j): self.clusters[i]+=self.clusters[j] self.clusters.pop(j)
Когда я запускаю:
>>> x = np . array ([[0 , 0] , [0 , 0.1] , [1 , 1]]) >>> hc = lc . SingleLinkage (x , 2) >>> hc . clusters
>>> hc . clusters {0: [0], 1: [1], 2: [2]}
{0: [0 , 1] , 1: [2]}