我正在尝试编写一个用于聚类的模块,但是类方法对我来说有点混乱
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)
当我运行它时,它给了我:
>>> x = np.array ([[0 , 0] , [0 , 0.1] , [1 , 1]])
>>> hc = lc.SingleLinkage (x , 2)
>>> hc.clusters
{0: [0], 1: [1], 2: [2]}
但应该是不同的:
>>> hc.clusters
{0: [0 , 1] , 1: [2]}
我知道错误可能在init函数中。我该怎么改变它?