Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

FIX Stop EfficiencyWarnings in DBSCAN #31337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
Loading
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions 16 sklearn/cluster/_dbscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause

import warnings
from numbers import Integral, Real

import numpy as np
Expand Down Expand Up @@ -399,14 +398,6 @@ def fit(self, X, y=None, sample_weight=None):
# Calculate neighborhood for all samples. This leaves the original
# point in, which needs to be considered later (i.e. point i is in the
# neighborhood of point i. While True, its useless information)
if self.metric == "precomputed" and sparse.issparse(X):
# set the diagonal to explicit values, as a point is its own
# neighbor
X = X.copy() # copy to avoid in-place modification
with warnings.catch_warnings():
warnings.simplefilter("ignore", sparse.SparseEfficiencyWarning)
X.setdiag(X.diagonal())

neighbors_model = NearestNeighbors(
radius=self.eps,
algorithm=self.algorithm,
Expand All @@ -420,6 +411,13 @@ def fit(self, X, y=None, sample_weight=None):
# This has worst case O(n^2) memory complexity
neighborhoods = neighbors_model.radius_neighbors(X, return_distance=False)

# Each point is its own neighbor, so update the neighborhoods
# accordingly after the initial fitting
if self.metric == "precomputed" and sparse.issparse(X):
for i, neighborhood in enumerate(neighborhoods):
if i not in neighborhoods[i]:
neighborhoods[i] = np.append(neighborhood, i)
Comment on lines +414 to +419
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally, the distance of each point to itself is explicitly set in the sparse case to say "we have distance of 0 with myself".

Then L412 is rand to compute the neighborhoods.

Since L412 is now ran first, does this matter? I suspect not, but think we just want a few more eyes on this to prevent any form of a regression.


if sample_weight is None:
n_neighbors = np.array([len(neighbors) for neighbors in neighborhoods])
else:
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.