# K-Medoids in Julia - Results Quality

**URL:** <https://discourse.julialang.org/t/k-medoids-in-julia-results-quality/50687>\
**Category:** Machine Learning\
**Tags:** clustering\
**Created:** [November 24, 2020, 2:33pm UTC](https://discourse.julialang.org/t/k-medoids-in-julia-results-quality/50687 "2020-11-24T14:33:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [November 24, 2020, 2:33pm UTC](https://discourse.julialang.org/t/k-medoids-in-julia-results-quality/50687/1 "2020-11-24T14:33:13Z")

</div>

I was reading [the Wikipedia page on k-medoids clustering](https://en.wikipedia.org/wiki/K-medoids) and in the _Software_ section, it’s stated that:

> - [Julia](https://en.wikipedia.org/wiki/Julia_language) contains a k-medoid implementation of the k-means style algorithm (faster, but much worse result quality) in the [JuliaStats/Clustering.jl](https://github.com/JuliaStats/Clustering.jl) package.

I don’t have experience doing k-medoids with anything other than Clustering.jl.

- Can anyone comment on this assertion?
- Do k-medoids implementations in R/Python produce “better quality” results?
- I haven’t really dug into the source code at Clustering.jl yet, so does anyone know if it implements the standard PAM algorithm (described [here](https://www.datanovia.com/en/lessons/k-medoids-in-r-algorithm-and-practical-examples/))?

If I get some time I might do some simple comparisons between the Clustering.jl implementation and whatever I can cook up with JuMP.jl. I think I should just be able to compare [silhouette scores](https://juliastats.org/Clustering.jl/stable/validate.html#Silhouettes-1) between the two results.

EDIT: they addressed this in the docs:

> **Note** The function implements a _K-means style_ algorithm instead of _PAM_ (Partitioning Around Medoids). K-means style algorithm converges in fewer iterations, but was shown to produce worse (10-20% higher total costs) results (see e.g. [Schubert & Rousseeuw (2019)](https://juliastats.org/Clustering.jl/stable/kmedoids.html#kmedoid_refs-1)).

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 25, 2020, 9:02am UTC](https://discourse.julialang.org/t/k-medoids-in-julia-results-quality/50687/2 "2020-11-25T09:02:11Z")

</div>

If you don’t get a reply here, I think it is OK to ask in an issue at the package repo.

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [November 25, 2020, 12:29pm UTC](https://discourse.julialang.org/t/k-medoids-in-julia-results-quality/50687/3 "2020-11-25T12:29:58Z")

</div>

I think in the Algorithms section of the Wikipedia page, the algorithm is mentioned. I am not that aware of the other algorithms, but I know my k-means,  
so the mentioned Voronoi iteration is really similar to k-means in the sense of its steps.  
The basic difference is of course that k-means minimises distances squared (which has as closed form solution the mean) and Voronoi iteration minimises distances to the center.

_edit_: One reason of course is that the optimization problem involved here (with distances) is non smooth.

Mayer that answers your first part.  
I haven’t checked or compared other implementations/algorithms, but PAM would be nice to have for sure.

---

<div class="post-metadata">

**Author:** ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)\
**Post date:** [November 25, 2020, 5:18pm UTC](https://discourse.julialang.org/t/k-medoids-in-julia-results-quality/50687/4 "2020-11-25T17:18:37Z")

</div>

Found this, [https://www.cs.umb.edu/cs738/pam1.pdf](https://www.cs.umb.edu/cs738/pam1.pdf)  
Looks straight forward to implement.

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [November 25, 2020, 6:37pm UTC](https://discourse.julialang.org/t/k-medoids-in-julia-results-quality/50687/5 "2020-11-25T18:37:40Z")

</div>

Neat. Might be worth adding to `Clustering.jl`.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [November 25, 2020, 9:05pm UTC](https://discourse.julialang.org/t/k-medoids-in-julia-results-quality/50687/6 "2020-11-25T21:05:10Z")

</div>

An other imementation in Julia, altought pretty vanilla and not optimised:

[https://github.com/sylvaticus/BetaML.jl/blob/master/src/Clustering.jl](https://github.com/sylvaticus/BetaML.jl/blob/master/src/Clustering.jl)

(disclaimer, I am the author)

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [November 25, 2020, 9:09pm UTC](https://discourse.julialang.org/t/k-medoids-in-julia-results-quality/50687/7 "2020-11-25T21:09:23Z")

</div>

A parallel version is described here:

> **[sensors-18-04129.pdf](https://mdpi-res.com/d_attachment/sensors/sensors-18-04129/article_deploy/sensors-18-04129.pdf)**
>
> 1100.83 KB

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [November 27, 2020, 9:54pm UTC](https://discourse.julialang.org/t/k-medoids-in-julia-results-quality/50687/8 "2020-11-27T21:54:05Z")

</div>

It appears that PAM hasn’t been implemented in any of the public packages in Julia. I’ve found a few resources that include pseudocode for PAM:

- [This one by the original PAM authors](https://arxiv.org/pdf/1810.05691.pdf)
- [The one I already linked to above](https://discourse.julialang.org/t/k-medoids-in-julia-results-quality/50687/7)

So…who wants to help me write it?! 😆 In all seriousness, I’ve been playing around with it for the past couple of hours but I could use some guidance from someone more experience in this realm. I think I’ll start a new post specifically about PAM in Julia to get a discussion going on that particular topic.
