# Dbscan clustering with distance matrix

**URL:** https://discourse.julialang.org/t/dbscan-clustering-with-distance-matrix/72700
**Category:** General Usage
**Tags:** clustering
**Created:** [December 7, 2021, 8:32am UTC](https://discourse.julialang.org/t/dbscan-clustering-with-distance-matrix/72700 "2021-12-07T08:32:11Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![frits45](https://avatars.discourse-cdn.com/v4/letter/f/3be4f8/32.png) [@frits45](https://discourse.julialang.org/u/frits45)
#### Post date: [December 7, 2021, 8:32am UTC](https://discourse.julialang.org/t/dbscan-clustering-with-distance-matrix/72700/1 "2021-12-07T08:32:11Z")

</div>

Hello,  
I need to cluster “objects” that are not points in space, but I can calculate a distance between them. The documentation says:

> There are two implementations of DBSCAN algorithm in this package (both provided by dbscan function):  
> Distance (adjacency) matrix-based. It requires O(N2)O(N2) memory to run. Boundary points cannot be shared between the clusters.  
> …

I wrote a small script to test this, but it does not work. The clustering package only seems to accept points in a space, not a precomputed distance matrix. Is it possible to cluster with a distance matrix?

```julia
using Clustering
points = [1.0, 2.0, 3.0, 4.0, 7.0, 8.0, 9.0, 10.0]
s = size(points)[1]
dists = zeros(s,s)
for k in 1:s
    for l in k+1:s
        d = sqrt((points[k] - points[l])^2)
        dists[k,l] = d
        dists[l,k] = d
    end
end
r = 2.0
size, core, boundary = dbscan(dists, r, min_cluster_size = 3)
println(size)

```

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [December 7, 2021, 9:36am UTC](https://discourse.julialang.org/t/dbscan-clustering-with-distance-matrix/72700/2 "2021-12-07T09:36:36Z")

</div>

```julia
function dbscan(D::AbstractMatrix{T}, eps::Real, minpts::Int) where T<:Real 

```

Try supplying the third integer argument so that dispatch works correctly.

---

<div class="post-metadata">

### Author: ![frits45](https://avatars.discourse-cdn.com/v4/letter/f/3be4f8/32.png) [@frits45](https://discourse.julialang.org/u/frits45)
#### Post date: [December 7, 2021, 9:42am UTC](https://discourse.julialang.org/t/dbscan-clustering-with-distance-matrix/72700/3 "2021-12-07T09:42:43Z")

</div>

Thanks for replying. The error message changed; I now get a Julia 1.7.0. error. The code I used is:  
`size, core, boundary = dbscan(dists, r, 3)`  
The error (that I do not understand):

```julia
Exception has occurred: MethodError
MethodError: no method matching iterate(::DbscanResult)
Closest candidates are:
  iterate(!Matched::Union{LinRange, StepRangeLen}) at /usr/local/julia-1.7.0/share/julia/base/range.jl:826
  iterate(!Matched::Union{LinRange, StepRangeLen}, !Matched::Integer) at /usr/local/julia-1.7.0/share/julia/base/range.jl:826
  iterate(!Matched::T) where T<:Union{Base.KeySet{<:Any, <:Dict}, Base.ValueIterator{<:Dict}} at /usr/local/julia-1.7.0/share/julia/base/dict.jl:695

```

…

---

<div class="post-metadata">

### Author: ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)
#### Post date: [December 7, 2021, 10:00am UTC](https://discourse.julialang.org/t/dbscan-clustering-with-distance-matrix/72700/4 "2021-12-07T10:00:59Z")

</div>

You could try `(;seeds, assignments, counts) = dbscan(dists, r,3)` with julia 1.7  
This version of `dbscan` returns a `DbscanResult` while the other implementation return a `DbscanCluster`. These do not behave the same…

---

<div class="post-metadata">

### Author: ![frits45](https://avatars.discourse-cdn.com/v4/letter/f/3be4f8/32.png) [@frits45](https://discourse.julialang.org/u/frits45)
#### Post date: [December 7, 2021, 10:06am UTC](https://discourse.julialang.org/t/dbscan-clustering-with-distance-matrix/72700/5 "2021-12-07T10:06:38Z")

</div>

Thank you. The error message changed again, and has become minimal:

```julia
Exception has occurred: ErrorException
type DbscanResult has no field size

Stacktrace:
 [1] top-level scope
   @ ~/juliatest/dbscantest.jl:13

```

I hope one more suggestion provides working code

---

<div class="post-metadata">

### Author: ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)
#### Post date: [December 7, 2021, 11:42am UTC](https://discourse.julialang.org/t/dbscan-clustering-with-distance-matrix/72700/6 "2021-12-07T11:42:31Z")

</div>

The 1.7 syntax `(;seeds, assignments, counts) = dbscan(dists, r,3)` is equivalent to

```julia
rst = dbscan(dists,r,3)
seeds = rst.seeds
assignments = rst.assignments
counts = rst.counts

```

It only works to expose fields of a structure by their names. Therefore `(;size, core,boundary)= dbscan(dists, r,3) ` fails because there are no such fields in a `DbscanResult`

---

<div class="post-metadata">

### Author: ![frits45](https://avatars.discourse-cdn.com/v4/letter/f/3be4f8/32.png) [@frits45](https://discourse.julialang.org/u/frits45)
#### Post date: [December 7, 2021, 11:51am UTC](https://discourse.julialang.org/t/dbscan-clustering-with-distance-matrix/72700/7 "2021-12-07T11:51:37Z")

</div>

Thanks! 😃
