<jupyter_start><jupyter_text>🍔 K Means Clustering Step By Step  Installation<jupyter_code># remove `!` if running the line in a terminal
!pip install -U RelevanceAI[notebook]==2.0.0<jupyter_output><empty_output><jupyter_text>Setup First, you need to set up a client object to interact with RelevanceAI.<jupyter_code>from relevanceai import Client

client = Client()<jupyter_output><empty_output><jupyter_text>Data You will need to have a dataset under your Relevance AI account. You can either use our e-commerce dataset as shown below or follow the tutorial on how to create your own dataset.Our e-commerce dataset includes fields such as `product_title`, as well as the vectorized version of the field `product_title_clip_vector_`. Loading these documents can be done via:  Load the data<jupyter_code>from relevanceai.utils.datasets import get_ecommerce_dataset_encoded

documents = get_ecommerce_dataset_encoded()
{k: v for k, v in documents[0].items() if "_vector_" not in k}<jupyter_output><empty_output><jupyter_text>Upload the data to Relevance AI Run the following cell, to upload these documents into your personal Relevance AI account under the name `quickstart_clustering_kmeans`<jupyter_code>ds = client.Dataset("quickstart_kmeans_clustering")
ds.insert_documents(documents)<jupyter_output><empty_output><jupyter_text>Check the data<jupyter_code>ds.health()
ds.schema<jupyter_output><empty_output><jupyter_text>Clustering We apply the Kmeams clustering algorithm to the vector field, `product_title_clip_vector_`<jupyter_code>from sklearn.cluster import KMeans

VECTOR_FIELD = "product_title_clip_vector_"
KMEAN_NUMBER_OF_CLUSTERS = 5
ALIAS = "kmeans_" + str(KMEAN_NUMBER_OF_CLUSTERS)

model = KMeans(n_clusters=KMEAN_NUMBER_OF_CLUSTERS)
clusterer = client.ClusterOps(alias=ALIAS, model=model)
clusterer.run(
    dataset_id="quickstart_kmeans_clustering",
    vector_fields=["product_title_clip_vector_"],
)
# List closest to center of the cluster

clusterer.list_closest(
    dataset_id="quickstart_kmeans_clustering", vector_field="product_title_clip_vector_"
)
# List furthest from the center of the cluster

clusterer.list_furthest(
    dataset_id="quickstart_kmeans_clustering", vector_field="product_title_clip_vector_"
)<jupyter_output><empty_output><jupyter_text>We download a small sample and show the clustering results using our json_shower.<jupyter_code>from relevanceai import show_json

sample_documents = ds.sample(n=5)
samples = [
    {
        "product_title": d["product_title"],
        "cluster": d["_cluster_"][VECTOR_FIELD][ALIAS],
    }
    for d in sample_documents
]

show_json(samples, text_fields=["product_title", "cluster"])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08200371057513915, 'fraction_numerical': 0.0022263450834879408, 'mean_word_length': 7.425, 'pattern_counts': {'":': 2, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '667769', 'license': 'Apache-2.0', 'n_tokens_mistral': 902, 'n_tokens_neox': 868, 'n_words': 223, 'path': 'guides/kmeans_clustering_step_by_step_guide.ipynb', 'repo_name': 'RelevanceAI/RelevanceAI', 'size': '2695'}