Custom Clustering

How to perform custom clustering

RelevanceAI supports the integration of custom clustering algorithms. The custom algorithm can be created as the fit_transform method of the ClusterBase class.

What you will need

You will need to have followed the quickstart clustering guide.

Code Example

Open In ColabOpen In Colab

The following code shows an example of a custom clustering algorithm that chooses randomly between Cluster 0 and Cluster 1.

# Inherit from ClusterBase to keep all the goodies! 
from relevanceai.vector_tools.cluster import ClusterBase
class CustomCluster(ClusterBase):
    def fit_transform(self, vectors):
        import random
        return [random.randint(0, 1) for i in vectors]
clusterer = CustomCluster()

# Fit documents
custom_documents = clusterer.fit_documents(
  vector_fields = [CUSTOM_VECTOR_FIELD], 
  documents = custom_documents,
  # If True, return only clusters to be updated
  return_only_clusters = True 

# Update documents
client.update_documents(DATASET_ID, custom_documents)

Did this page help you?