How to Perform Hierarchical Clustering in Julia
Explanation
Use the hclust()
function from the Clustering.jl
package.
hclust(d::AbstractMatrix; [linkage], [uplo], [branchorder])
It takes a distance matrix as input and returns the result of hierarchical clustering. The default method for calculating distances between clusters is single linkage.
To plot a dendrogram, use StatsPlots.jl
instead of Plots.jl
.
Code
using StatsPlots
using Clustering
using Distances
using Distributions
a = rand(Uniform(-1,1), 2, 25)
scatt = scatter(a[1,:], a[2,:], label=false)
savefig(scatt, "julia_hclust_scatter.png")
D_a = pairwise(Euclidean(), a, a)
SL = hclust(D_a, linkage=:single)
plot_SL = plot(SL)
p = plot(scatt, plot_SL, size=(800,400))
savefig(p, "julia_hclust.png")