MCPcopy Create free account
hub / github.com/KnowledgeXLab/LeanRAG / perform_clustering

Function perform_clustering

_cluster_utils.py:244–283  ·  view source on GitHub ↗
(
    embeddings: np.ndarray, dim: int, threshold: float, verbose: bool = False,cluster_size: int = 20
)

Source from the content-addressed store, hash-verified

242
243
244def perform_clustering(
245 embeddings: np.ndarray, dim: int, threshold: float, verbose: bool = False,cluster_size: int = 20
246) -> List[np.ndarray]:
247 reduced_embeddings_global = global_cluster_embeddings(embeddings, min(dim, len(embeddings) -2))
248 global_clusters, n_global_clusters = GMM_cluster( # (num, 2)
249 reduced_embeddings_global, threshold,cluster_size=cluster_size
250 )
251 if len(global_clusters) != len(embeddings):
252 print('debug')
253 # if verbose:
254 # logging.info(f"Global Clusters: {n_global_clusters}")
255
256 # all_clusters = [[] for _ in range(len(embeddings))]
257 # embedding_to_index = {tuple(embedding): idx for idx, embedding in enumerate(embeddings)}
258 # for i in tqdm(range(n_global_clusters)):
259 # global_cluster_embeddings_ = embeddings[
260 # np.array([i in gc for gc in global_clusters])
261 # ] #找到当前簇的embedding
262 # if verbose:
263 # logging.info(
264 # f"Nodes in Global Cluster {i}: {len(global_cluster_embeddings_)}"
265 # )
266 # if len(global_cluster_embeddings_) == 0:
267 # continue
268
269 # # embedding indices #反向取idx
270 # indices = [
271 # embedding_to_index[tuple(embedding)]
272 # for embedding in global_cluster_embeddings_
273 # ]
274
275 # # update
276 # for idx in indices:
277 # all_clusters[idx].append(i)
278
279 # all_clusters = [np.array(cluster) for cluster in all_clusters]
280
281 # if verbose:
282 # logging.info(f"Total Clusters: {len(n_global_clusters)}")
283 return global_clusters
284
285def enclose_string_with_quotes(content: Any) -> str:
286 """Enclose a string with quotes"""

Callers 1

perform_clusteringMethod · 0.85

Calls 2

GMM_clusterFunction · 0.85

Tested by

no test coverage detected