Repository navigation
Fix Scholar.Cluster.DBSCAN :weights - #371
Merged
Merged
Conversation
The :weights tensor was broadcast along the wrong axis, so the density of a sample came out as its own weight times the size of its neighborhood, instead of the sum of the weights inside that neighborhood. Core points and clusters were wrong whenever the weights were not uniform, and nothing raised. scikit-learn sums sample_weight over the neighborhood, which is also what the :min_samples documentation already describes as the total weight.
A tensor given as an option is inlined into the expression rather than becoming a parameter, so fitting with :weights while the default backend is a device backend raised Nx.Defn.IncompatibleBackendsError. K-Means already validates its weights in the transform and forwards them as an argument, so DBSCAN now does the same.
josevalim
approved these changes
Oct 21, 2026
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.
Two problems with
:weights.The weight lands on the query point instead of its neighborhood. It is broadcast along the wrong axis, so a sample's density is its own weight times the size of its neighborhood, not the sum of the weights inside it. Any non uniform weight gives wrong core points and clusters, silently. scikit-learn sums
sample_weightover the neighborhood, which is what the:min_samplesdocs already call the total weight.A tensor given as an option is inlined into the expression, so
:weightson a device backend raisesNx.Defn.IncompatibleBackendsError. K-Means already validates its weights in the transform and forwards them as an argument, so DBSCAN does the same now.Checked against scikit-learn 1.6.1 on 900 random datasets (1 to 3 features, uniform, discrete and negative weights, varying
epsandmin_samples), comparing the core mask, the noise mask and the partition of core samples: 333/900 before, 900/900 after, same under EXLA.