Skip to content

Commit

Permalink
0 silhouette score set for clusers of size 1 (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
montyvesselinov authored and ararslan committed Jul 9, 2018
1 parent d076fd8 commit 7c5034b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/silhouette.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ function sil_aggregate_dists(k::Int, a::AbstractVector{Int}, dists::DenseMatrix{
end


function silhouettes(assignments::Vector{Int},
counts::AbstractVector{Int},
function silhouettes(assignments::Vector{Int},
counts::AbstractVector{Int},
dists::DenseMatrix{T}) where T<:Real

n = length(assignments)
Expand Down Expand Up @@ -69,13 +69,17 @@ function silhouettes(assignments::Vector{Int},
b[j] = v
end

# compute silhouette score
# compute silhouette score
sil = a # reuse the memory of a for sil
for j = 1:n
@inbounds sil[j] = (b[j] - a[j]) / max(a[j], b[j])
if counts[assignments[j]] == 1
sil[j] = 0
else
@inbounds sil[j] = (b[j] - a[j]) / max(a[j], b[j])
end
end
return sil
end

silhouettes(R::ClusteringResult, dists::DenseMatrix) =
silhouettes(assignments(R), counts(R), dists)
silhouettes(R::ClusteringResult, dists::DenseMatrix) =
silhouettes(assignments(R), counts(R), dists)
5 changes: 5 additions & 0 deletions test/silhouette.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ a = [1, 2, 1, 2]
c = [2, 2]

@test all(isapprox.(silhouettes(a, c, D), [0.0, -0.5, -0.5, 0.0]))

a = [1, 1, 1, 2]
c = [3, 1]

@test all(isapprox.(silhouettes(a, c, D), [0.5, 0.5, -1/3, 0.0]))

0 comments on commit 7c5034b

Please sign in to comment.