Skip to content

Conversation

rohitbansal2005
Copy link

Describe your change:

  • Add an algorithm

Implemented the K-Medoids clustering algorithm in Python.
This algorithm is similar to K-Means but uses actual data points as cluster centers (medoids),
making it more robust to noise and outliers.
Includes usage example, optional plotting, and doctest.

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes Implement K-Medoids Clustering Algorithm #13488".

@algorithms-keeper algorithms-keeper bot added require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html labels Oct 21, 2025
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

from matplotlib import pyplot as plt
from sklearn.metrics import pairwise_distances

def get_initial_medoids(data, k, seed=None):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/k_medoids.py, please provide doctest for the function get_initial_medoids

Please provide return type hint for the function: get_initial_medoids. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: data

Please provide descriptive name for the parameter: k

Please provide type hint for the parameter: k

Please provide type hint for the parameter: seed

medoids = data[indices, :]
return medoids

def assign_clusters(data, medoids):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/k_medoids.py, please provide doctest for the function assign_clusters

Please provide return type hint for the function: assign_clusters. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: data

Please provide type hint for the parameter: medoids

cluster_assignment = np.argmin(distances, axis=1)
return cluster_assignment

def revise_medoids(data, k, cluster_assignment):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/k_medoids.py, please provide doctest for the function revise_medoids

Please provide return type hint for the function: revise_medoids. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: data

Please provide descriptive name for the parameter: k

Please provide type hint for the parameter: k

Please provide type hint for the parameter: cluster_assignment

new_medoids.append(members[medoid_index])
return np.array(new_medoids)

def compute_heterogeneity(data, k, medoids, cluster_assignment):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/k_medoids.py, please provide doctest for the function compute_heterogeneity

Please provide return type hint for the function: compute_heterogeneity. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: data

Please provide descriptive name for the parameter: k

Please provide type hint for the parameter: k

Please provide type hint for the parameter: medoids

Please provide type hint for the parameter: cluster_assignment

heterogeneity += np.sum(distances**2)
return heterogeneity

def kmedoids(data, k, initial_medoids, maxiter=100, verbose=False):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/k_medoids.py, please provide doctest for the function kmedoids

Please provide return type hint for the function: kmedoids. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: data

Please provide descriptive name for the parameter: k

Please provide type hint for the parameter: k

Please provide type hint for the parameter: initial_medoids

Please provide type hint for the parameter: maxiter

Please provide type hint for the parameter: verbose

return medoids, cluster_assignment

# Optional plotting
def plot_clusters(data, medoids, cluster_assignment):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file machine_learning/k_medoids.py, please provide doctest for the function plot_clusters

Please provide return type hint for the function: plot_clusters. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: data

Please provide type hint for the parameter: medoids

Please provide type hint for the parameter: cluster_assignment

@algorithms-keeper algorithms-keeper bot added awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass labels Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html tests are failing Do not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement K-Medoids Clustering Algorithm

1 participant