def fit(self, feat):
# Compute affinity matrix using RBF kernel on pair-wise distances
affinity = scipy.spatial.distance.pdist(np.array([f for id, f in feat]))
sigma = -2 * np.var(affinity)
affinity = np.exp(scipy.spatial.distance.squareform(affinity) / sigma)
# Recursive clustering
self.tree = { 'depth' : 0, 'height' : 0, 'size' : 0, 'leafs' : 1, 'children' : [], 'parent' : None, 'items' : feat, 'affinity' : affinity }
queue = []
heapq.heappush(queue, (-1 * len(self.tree['items']), np.random.rand(), self.tree))
while (self.tree['leafs'] < self.max_clusters) and (len(queue) > 0):
if len(queue[0][2]['items']) <= self.min_cluster_size:
break
left, right, ncut_value = self.split(heapq.heappop(queue)[2])
if ncut_value > self.T:
break
if (left is not None) and (right is not None):
heapq.heappush(queue, (-1 * len(left['items']), np.random.rand(), left))
heapq.heappush(queue, (-1 * len(right['items']), np.random.rand(), right))
评论列表
文章目录