Repository navigation
[MRG] Use the weights w in ot.dist for all metrics - #884
Open
raashish1601 wants to merge 2 commits into
Open
raashish1601 wants to merge 2 commits into
raashish1601 wants to merge 2 commits into
Conversation
This branch has not been deployed
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.
Types of changes
Motivation and context / Related issue
ot.distsilently ignoredwfor every metric exceptcityblockandminkowski, so it returned the unweighted distance:This affected the backend implementations of
sqeuclidean,euclidean,cosineandcorrelation, and the numpy fallback tocdist, which only passedwforminkowski/wminkowski. Withbackend="scipy"the weights were used, so the result depended on the backend.test_distalready lists these metrics as "those that support weights" but did not check the values. #859 fixed the same problem forcityblock.Changes:
sqeuclidean/euclidean: scale both point sets bysqrt(w)beforeeuclidean_distances.cosine: same scaling.correlation: center with the weighted mean, then the same scaling. This matches the scipy definitions.wtocdistwhenever it is given (scipy still raises for metrics that do not accept weights).Unweighted results are unchanged.
How has this been tested (if it applies)
Added
test_dist_weighted_vs_cdist(numpy, compared withscipy.spatial.distance.cdistfor sqeuclidean, euclidean, cosine, correlation, braycurtis and canberra) andtest_dist_weighted_backends(all backends, for the four backend metrics). All new cases fail on master.pytest test/test_utils.pypasses (numpy and torch backends installed locally), and ruff 0.5.2 lint and format are clean.PR checklist