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

Function get_optimal_clusters

_cluster_utils.py:213–227  ·  view source on GitHub ↗
(embeddings, max_clusters=50, random_state=0, rel_tol=1e-3)

Source from the content-addressed store, hash-verified

211
212
213def get_optimal_clusters(embeddings, max_clusters=50, random_state=0, rel_tol=1e-3):
214 max_clusters = min(len(embeddings), max_clusters)
215 n_clusters = np.arange(1, max_clusters)
216 bics = []
217 prev_bic = float('inf')
218 for n in tqdm(n_clusters):
219 bic = fit_gaussian_mixture(n, embeddings, random_state)
220 # print(bic)
221 bics.append(bic)
222 # early stop
223 if (abs(prev_bic - bic) / abs(prev_bic)) < rel_tol:
224 break
225 prev_bic = bic
226 optimal_clusters = n_clusters[np.argmin(bics)]
227 return optimal_clusters
228
229
230def GMM_cluster(embeddings: np.ndarray, threshold: float, random_state: int = 0,cluster_size: int = 20):

Callers 1

GMM_clusterFunction · 0.85

Calls 1

fit_gaussian_mixtureFunction · 0.85

Tested by

no test coverage detected