# Clustering.jl Silhouettes Distances

**URL:** https://discourse.julialang.org/t/clustering-jl-silhouettes-distances/33810
**Category:** General Usage
**Tags:** k-means
**Created:** [January 26, 2020, 2:34pm UTC](https://discourse.julialang.org/t/clustering-jl-silhouettes-distances/33810 "2020-01-26T14:34:06Z")
**Posts on this page:** 9
**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: [January 26, 2020, 2:34pm UTC](https://discourse.julialang.org/t/clustering-jl-silhouettes-distances/33810/1 "2020-01-26T14:34:06Z")

</div>

[The docs state:](https://juliastats.org/Clustering.jl/stable/validate.html)

> Note that si≤1, and that si is close to 1 when the i-th point lies well within its own cluster. This property allows using `mean(silhouettes(assignments, counts, X))` as a measure of clustering quality. Higher values indicate better separation of clusters w.r.t. point distances.

Elsewhere in the documentation, `X` is used to denote the data matrix. However, the following produces an error:

```julia
using Clustering

X = rand(5, 1000)
R = kmeans(X, 20, maxiter=200, display=:iter)

a = assignments(R)
c = counts(R)

mean(silhouettes(a, c, X))

julia> mean(silhouettes(a, c, X))
ERROR: DimensionMismatch("The size of a distance matrix ((5, 1000)) doesn't match the length of assignment vector (1000).")
Stacktrace:
 [1] silhouettes(::Array{Int64,1}, ::Array{Int64,1}, ::Array{Float64,2}) at C:\Users\mthel\.julia\packages\Clustering\Dlx92\src\silhouette.jl:55      
 [2] top-level scope at REPL[35]:1

```

Is this a bug or is X supposed to be the pairwise distance matrix (as the docs for the `silhouette` function indicate)? If the latter is true, is there a recommended way to compute the pairwise distance matrix?

---

<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: [January 26, 2020, 2:44pm UTC](https://discourse.julialang.org/t/clustering-jl-silhouettes-distances/33810/2 "2020-01-26T14:44:56Z")

</div>

This seems to work fine, but I might suggest an update to the docs to make it a bit clearer. I’m also still interested in feedback regarding a “recommended” way to do this:

```julia
using Clustering
using Distances
using Statistics

X = rand(5, 1000)
R = kmeans(X, 30, maxiter=200, display=:iter)

a = assignments(R)
c = counts(R)
M = R.centers
distances = pairwise(SqEuclidean(), X)

julia> mean(silhouettes(a, c, distances))
0.2992654354073209

```

---

<div class="post-metadata">

### Author: ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)
#### Post date: [July 18, 2022, 9:45am UTC](https://discourse.julialang.org/t/clustering-jl-silhouettes-distances/33810/3 "2022-07-18T09:45:56Z")

</div>

@mthelm85 For me, it says:

```nohighlight
ERROR: MethodError: objects of type Vector{Float64} are not callable
Use square brackets [] for indexing an Array.
Stacktrace:
 [1] top-level scope
   @ c:\Users\Shayan\Desktop\AUT\Thesis\MyWork\Thesis.jl:261

```

And the line `261` is : `mean(silhouettes(a, c, distances))`

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [July 18, 2022, 10:08am UTC](https://discourse.julialang.org/t/clustering-jl-silhouettes-distances/33810/4 "2022-07-18T10:08:50Z")

</div>

Please don’t resurrect old threads, and make an MWE with your question as per [this post](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757).

It’s likely that you just re-bound `mean` (or `silhouettes`) to a number somewhere in your code:

```julia
julia> using Statistics

julia> mean = 5
5

julia> mean(rand(10))
ERROR: MethodError: objects of type Int64 are not callable

```

Matt’s code from the post above works just fine for me in a new session.

---

<div class="post-metadata">

### Author: ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)
#### Post date: [July 18, 2022, 10:12am UTC](https://discourse.julialang.org/t/clustering-jl-silhouettes-distances/33810/5 "2022-07-18T10:12:59Z")

</div>

Ok, So probably I should open a new topic since the shape of my data is different?

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [July 18, 2022, 10:14am UTC](https://discourse.julialang.org/t/clustering-jl-silhouettes-distances/33810/6 "2022-07-18T10:14:26Z")

</div>

I don’t think the error has anything to do with the shape of your data, but yes do open another topic where you produce an MWE, which means:

1. Create a temp environment
2. Add the packages you need
3. Create some dummy data
4. Run the code that produces the error you’re seeing

Then copy-paste the code from steps 3 and 4 into your new question.

---

<div class="post-metadata">

### Author: ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)
#### Post date: [July 18, 2022, 10:17am UTC](https://discourse.julialang.org/t/clustering-jl-silhouettes-distances/33810/7 "2022-07-18T10:17:31Z")

</div>

Thanks for this great explanation. How can I create a temp env? What is a temp env, BTW?

And can you please check [my question](https://discourse.julialang.org/t/kmeans-clustering-using-clustering-package/84391)? I should know about that first; Then I can open a new topic for the latest issue.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [July 18, 2022, 10:58am UTC](https://discourse.julialang.org/t/clustering-jl-silhouettes-distances/33810/8 "2022-07-18T10:58:45Z")

</div>

In the REPL, hitting `]` gets you into Pkg mode, then do `activate --temp`

---

<div class="post-metadata">

### Author: ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)
#### Post date: [July 18, 2022, 11:04am UTC](https://discourse.julialang.org/t/clustering-jl-silhouettes-distances/33810/9 "2022-07-18T11:04:03Z")

</div>

Thanks! And the problem solved! I had a variable named `silhouettes`, so Julia got confused. Thanks a lot.
