Skip to content

Commit

Permalink
Set default preference to be median similarity of all pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuan-Ru-Lin committed Dec 3, 2024
1 parent c2f5d18 commit 7d0453c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ Clustering = "aaaa29a8-35af-508c-8bc3-b662a17a0fe5"
Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MLJModelInterface = "e80e1ace-859a-464e-9ed9-23947d8ae3ea"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"

[compat]
Clustering = "0.15"
Distances = "0.9, 0.10"
LinearAlgebra = "1.11.0"
MLJModelInterface = "1.4"
StatsBase = "0.34.3"
julia = "1.6"
14 changes: 11 additions & 3 deletions src/MLJClusteringInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import MLJModelInterface: Continuous, Count, Finite, Multiclass, Table, OrderedF

using Distances
using LinearAlgebra
using StatsBase

# ===================================================================
## EXPORTS
Expand Down Expand Up @@ -224,11 +225,18 @@ function MMI.predict(model::AffinityPropagation, ::Nothing, X)
# Compute similarity matrix using negative pairwise distances
S = -pairwise(model.metric, Xarray, dims=2)

Check warning on line 226 in src/MLJClusteringInterface.jl

View check run for this annotation

Codecov / codecov/patch

src/MLJClusteringInterface.jl#L226

Added line #L226 was not covered by tests

# Set preferences on diagonal if specified
if !isnothing(model.preference)
fill!(view(S, diagind(S)), model.preference)
diagonal_element = if !isnothing(model.preference)
model.preference

Check warning on line 229 in src/MLJClusteringInterface.jl

View check run for this annotation

Codecov / codecov/patch

src/MLJClusteringInterface.jl#L228-L229

Added lines #L228 - L229 were not covered by tests
else
# Get the median out of all pairs of similarity, that is, values above
# the diagonal line.
# Such default choice is mentioned in the algorithm's wiki article
iuppertri = triu!(trues(size(S)),1)
median(S[iuppertri])

Check warning on line 235 in src/MLJClusteringInterface.jl

View check run for this annotation

Codecov / codecov/patch

src/MLJClusteringInterface.jl#L234-L235

Added lines #L234 - L235 were not covered by tests
end

fill!(view(S, diagind(S)), diagonal_element)

Check warning on line 238 in src/MLJClusteringInterface.jl

View check run for this annotation

Codecov / codecov/patch

src/MLJClusteringInterface.jl#L238

Added line #L238 was not covered by tests

result = Cl.affinityprop(

Check warning on line 240 in src/MLJClusteringInterface.jl

View check run for this annotation

Codecov / codecov/patch

src/MLJClusteringInterface.jl#L240

Added line #L240 was not covered by tests
S,
maxiter=model.maxiter,
Expand Down

0 comments on commit 7d0453c

Please sign in to comment.