# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# ## dbscan clustering using `cuml`
#

# +
# %load_ext autoreload
# %autoreload 2

import utils
from cuml.cluster import DBSCAN

import numpy as np

# +
# load embeddings matrix using KGEmbeddingStore class
emb_store = utils.load_embedding_store()
X = emb_store.ent_embedding_matrix
dim = X.shape[1]

dim

# + tags=[]
labels = {}

for EPS in [0.25, 0.5, 0.75]:
    print(f"EPS = {EPS}")
    
    dbscan_float = DBSCAN(
        eps=EPS, 
        min_samples=2,
        verbose=False,
    )
    labels[EPS] = dbscan_float.fit_predict(X)
    print(f"no clusters = {len(np.unique(labels[EPS])) - 1}")
    
    with open(f"./dbscan_cluster_idxs_EPS_{EPS}.txt", "wb") as f:
        np.savetxt(f, labels[EPS].astype(int), fmt='%i', delimiter=",")
    
# -

len(np.unique(labels[EPS])) - 1



StarcoderdataJupyterScriptsDedupFiltered