Skip to content

Fix Scholar.Cluster.DBSCAN :weights - #371

Merged
josevalim merged 2 commits into
elixir-nx:mainfrom
RicardoSantos-99:fix-dbscan-weights
Oct 21, 2026
Merged

josevalim merged 2 commits into
elixir-nx:mainfrom
RicardoSantos-99:fix-dbscan-weights

Conversation

@RicardoSantos-99

Copy link
Copy Markdown
Contributor

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_weight over the neighborhood, which is what the :min_samples docs already call the total weight.

A tensor given as an option is inlined into the expression, so :weights on a device backend raises Nx.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 eps and min_samples), comparing the core mask, the noise mask and the partition of core samples: 333/900 before, 900/900 after, same under EXLA.

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
josevalim merged commit 8e91c1e into elixir-nx:main Oct 21, 2026
2 checks passed
@RicardoSantos-99
RicardoSantos-99 deleted the fix-dbscan-weights branch October 21, 2026 10:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants