CoolFace
Apppublic

radames/sentence-embeddings-visualization

sourceHugging Faceupdated 3y agoView on Hugging Face
19likes
umap_reducer.py28 linesDownload Raw Back to root
1import umap2import hdbscan3import copy4 5 6class UMAPReducer:7    def __init__(self, umap_options={}, cluster_options={}):8 9        # set options with defaults10        self.umap_options = {'n_components': 2, 'spread': 1, 'min_dist': 0.1, 'n_neighbors': 15,11                             'metric': 'cosine', "verbose": True, **umap_options}12        self.cluster_options = {'allow_single_cluster': True, 'min_cluster_size': 500, 'min_samples': 10, **cluster_options}13 14    def setParams(self, umap_options={}, cluster_options={}):15        # update params16        self.umap_options = {**self.umap_options, **umap_options}17        self.cluster_options = {**self.cluster_options, **cluster_options}18 19    def clusterAnalysis(self, data):20        print("Cluster params:", self.cluster_options)21        clusters = hdbscan.HDBSCAN().fit(data) # **self.cluster_options22        return clusters23 24    def embed(self, data):25        print("UMAP params:", self.umap_options)26        result = umap.UMAP(**self.umap_options).fit_transform(data)27        return result28