# DimensionMismatch: The size of a distance matrix ((1, 440)) doesn't match the length of assignment vector (440)

**URL:** https://discourse.julialang.org/t/dimensionmismatch-the-size-of-a-distance-matrix-1-440-doesnt-match-the-length-of-assignment-vector-440/90807
**Category:** Machine Learning
**Tags:** clustering
**Created:** [November 25, 2022, 1:41pm UTC](https://discourse.julialang.org/t/dimensionmismatch-the-size-of-a-distance-matrix-1-440-doesnt-match-the-length-of-assignment-vector-440/90807 "2022-11-25T13:41:35Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![JUL1A](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jul1a/32/47432_2.png) [@JUL1A](https://discourse.julialang.org/u/JUL1A)
#### Post date: [November 25, 2022, 1:41pm UTC](https://discourse.julialang.org/t/dimensionmismatch-the-size-of-a-distance-matrix-1-440-doesnt-match-the-length-of-assignment-vector-440/90807/1 "2022-11-25T13:41:35Z")

</div>

How to fix this:

```julia
using Clustering
using VectorizedStatistics

function MyKmeans(Train::Vector{Float64}, k::Int64)
    the_mat = Train'
    model = kmeans(the_mat, k)

    return vmean(silhouettes(assignments(model), counts(model), the_mat))
end

```

```julia
julia> MyKmeans(rand(440), 2)
ERROR: DimensionMismatch: The size of a distance matrix ((1, 440)) doesn't match the length of assignment vector 
(440).
Stacktrace:
 [1] silhouettes(assignments::Vector{Int64}, counts::Vector{Int64}, dists::Adjoint{Float64, Vector{Float64}})    
   @ Clustering C:\Users\Julia\.julia\packages\Clustering\eBCMN\src\silhouette.jl:55
 [2] MyKmeans(Train::Vector{Float64}, k::Int64, kind::Silhouette)
   @ Main e:\newapproach.jl:155
 [3] top-level scope
   @ REPL[33]:1

```

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [November 25, 2022, 2:14pm UTC](https://discourse.julialang.org/t/dimensionmismatch-the-size-of-a-distance-matrix-1-440-doesnt-match-the-length-of-assignment-vector-440/90807/2 "2022-11-25T14:14:55Z")

</div>

`the_mat` needs to be a distance matrix. So, it can be initialized as:

```julia
    the_mat = abs.(Train .- Train')

```

and then the function returns without an error.

Another option for getting the same distance matrix (in a more forward looking programming style):

```julia
    the_mat = pairwise(Euclidean(), Train)

```

---

<div class="post-metadata">

### Author: ![JUL1A](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jul1a/32/47432_2.png) [@JUL1A](https://discourse.julialang.org/u/JUL1A)
#### Post date: [November 25, 2022, 2:32pm UTC](https://discourse.julialang.org/t/dimensionmismatch-the-size-of-a-distance-matrix-1-440-doesnt-match-the-length-of-assignment-vector-440/90807/3 "2022-11-25T14:32:28Z")

</div>

Thanks,  
Should I give the result of `abs.(Train .- Train')` to `kmeans` function rather than `Train'`? Or just in case of `silhouettes`?

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [November 25, 2022, 3:36pm UTC](https://discourse.julialang.org/t/dimensionmismatch-the-size-of-a-distance-matrix-1-440-doesnt-match-the-length-of-assignment-vector-440/90807/4 "2022-11-25T15:36:13Z")

</div>

Just `silhouettes` function, `kmeans` likes `Train'`.  
BTW, if you are new you might have missed out on the easy ‘help’ feature of the REPL: Just pressing `?` and a function gives useful info for reputable packages.
