Skip to content

FIX bug indices medoids CLARA #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sklearn_extra/cluster/_k_medoids.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ def fit(self, X, y=None):
medoids_idxs = pam.medoid_indices_
best_sample_idxs = sample_idxs

self.medoid_indices_ = medoids_idxs
self.medoid_indices_ = sample_idxs[medoids_idxs]
self.labels_ = np.argmin(self.transform(X), axis=1)
self.n_iter_ = self.n_sampling_iter

Expand Down
19 changes: 19 additions & 0 deletions sklearn_extra/cluster/tests/test_k_medoids.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,22 @@ def test_seuclidean():
km.predict(np.array([0, 0, 0, 1]).reshape((4, 1)))
km.transform(np.array([0, 0, 0, 1]).reshape((4, 1)))
assert len(record) == 0


def test_medoids_indices():
rng = np.random.RandomState(seed)
X_iris = load_iris()["data"]

clara = CLARA(
n_clusters=3,
n_sampling_iter=1,
n_sampling=len(X_iris),
random_state=rng,
)

model = KMedoids(n_clusters=3, init="build", random_state=rng)

model.fit(X_iris)
clara.fit(X_iris)
assert_array_equal(X_iris[model.medoid_indices_], model.cluster_centers_)
assert_array_equal(X_iris[clara.medoid_indices_], clara.cluster_centers_)