Add Ward linkage to hierarchical clustering - #336
Merged
Conversation
Ward linkage was disabled in the parallel nearest-neighbor dendrogram
because it produced incorrect dissimilarities. Two things were missing:
* Ward's Lance-Williams recurrence needs the size of the third cluster
(the one whose distance is being updated), which differs per column of
the pairwise matrix. The other linkages only need the sizes of the two
clusters being merged, so those sizes were the only ones threaded
through merge_clades. We now also track the per-cluster sizes and pass
them to the update function.
* The recurrence runs on squared distances, so the update squares the
pairwise distances before combining them and takes the square root of
the result.
With both in place, the dissimilarities match SciPy's
scipy.cluster.hierarchy.linkage(method: "ward"). Re-enables the linkage
in fit/2, documents it, and restores the ward test.
Contributor
Author
josevalim
approved these changes
Jul 11, 2026
Contributor
|
💚 💙 💜 💛 ❤️ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Adds
:wardlinkage support toScholar.Cluster.Hierarchical.The
ward/6update computes the new distances on squared distances, andmerge_cladesnow tracks the per-cluster sizes so the update has the size ofeach remaining cluster (which Ward needs, unlike the other linkages).
Enables
:wardinfit/2, adds it to the docs, and includes awardtestagainst the SciPy reference values.
Related to #213