<jupyter_start><jupyter_text>K-MedoidsIn order to use any distance measure and still ensure convergence, one can use K-Medoids. One can view K-Medoids as the most general version of K-Means.K-Medoids will converge regardless of the distance measure used, so in this example, we’ll use cosine distance.<jupyter_code>import sys ; sys.path.append("D:/source/skratch/source")
import numpy as np
import copy
 
from unsupervised.kmeans import KMeans
from utils.distances import pdist, euclidean, manhattan, cosine
 
 
class KMedoids(KMeans):
 
    def _distance(self, a, b):
 
        return cosine(a, b)<jupyter_output><empty_output><jupyter_text>Like K-Medians, K-Medoids requires a slightly different way to define the centroids. K-Medoids defines a centroid, in this case a medoid, as the most central point in a cluster. In practice, it means that every data point could potentially be a centroid, but the one minimizing the inertia the most is picked at each step.<jupyter_code>def _compute_centroids(self, X, labels):

        if not hasattr(self, "distances"):
            self.distances = pdist(X, self._distance)

        centroids = []

        for i in range(self.k):

            distances = self.distances[np.ix_(labels == i, labels == i)]
            within_cluster_sum_of_distances = np.sum(distances, axis=0)

            centroid = X[labels == i][np.argmin(within_cluster_sum_of_distances)]

            centroids.append(centroid)

        return np.array(centroids)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08355437665782493, 'fraction_numerical': 0.000663129973474801, 'mean_word_length': 3.805732484076433, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '614328', 'license': 'MIT', 'n_tokens_mistral': 473, 'n_tokens_neox': 451, 'n_words': 153, 'path': 'notebooks/Learning Units/K-Means/K-Means - Chapter 4 - K-Medoids.ipynb', 'repo_name': 'ValentinCalomme/skratch', 'size': '1508'}