Skip to content

Fix ValueError in KNeighborsClassifier with brute force and p=1 - #33048

Merged
jeremiedbb merged 5 commits into
scikit-learn:mainfrom
AAAZZZR:fix-issue-33034-knn-crash
May 13, 2026
Merged

jeremiedbb merged 5 commits into
scikit-learn:mainfrom
AAAZZZR:fix-issue-33034-knn-crash

Conversation

@AAAZZZR

@AAAZZZR AAAZZZR commented Jan 12, 2026

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Fixes #33034

What does this implement/fix? Explain your changes.

When using KNeighborsClassifier with algorithm="brute" and p=1 (Manhattan distance), the code path utilizes ArgKminClassMode. Previously, self.classes_ (which can contain strings) was passed directly to the unique_Y_labels argument of the Cython method, causing a ValueError when it attempted to interpret strings as integers.

I have updated the call to pass np.arange(len(self.classes_), dtype=np.intp) instead, which provides the correct integer indices expected by the underlying Cython implementation.

Any other comments?

Added a regression test case in sklearn/neighbors/tests/test_neighbors.py.

@github-actions github-actions Bot added module:neighbors CI:Linter failure The linter CI is failing on this PR labels Jan 12, 2026
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Jan 12, 2026

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR. From my analysis in the upstream issue, this fix sounds correct to me. Here is some feedback on the changelog entry and the test:

@@ -0,0 +1 @@
Fixed a :class:~sklearn.neighbors.KNeighborsClassifier failure when using algorithm='brute', p=1 and string labels.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

assert_allclose(loocv, nn_model.predict(None))


def test_kneighbors_classifier_brute_manhattan_string_labels():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Rather than specialize this test for the Manhattan distance metric, let's check that KNC works with string labels for any combination of algorithm and metric.

Suggested change
def test_kneighbors_classifier_brute_manhattan_string_labels():
@pytest.mark.parametrize("metric", COMMON_VALID_METRICS + DISTANCE_METRIC_OBJS)
@pytest.mark.parametrize("algorithm", ALGORITHMS)
def test_kneighbors_classifier_with_string_labels(metric, algorithm):

@ogrisel

ogrisel commented Jan 16, 2026

Copy link
Copy Markdown
Member

You might need to sync this branch with main to get the CI working again.

@ogrisel

ogrisel commented Jan 19, 2026 •

Copy link
Copy Markdown
Member

@AAAZZZR please update the PR with the feedback above. You might even take the following comment into account to implement a better fix: #33034 (comment), but the previous review feedback would still hold.

@yuanx749

Copy link
Copy Markdown
Contributor

RadiusNeighborsClassifier has the same issue. You might fix it while you're at it.

weights=self.weights,
Y_labels=self._y,
unique_Y_labels=self.classes_,
unique_Y_labels=np.arange(len(self.classes_), dtype=np.intp),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could apply the same fix to RadiusNeighborsClassMode.compute below.

@jeremiedbb jeremiedbb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I extended the test for all metrics and fixed RadiusNeighbors as well.
Radiusneighbors uses the unique_Y_labels values so I kept it for consistency. We can still revisit the idea of a refactoring later.

LGTM

@jeremiedbb
jeremiedbb enabled auto-merge (squash) May 13, 2026 22:33
@jeremiedbb
jeremiedbb merged commit c5ed670 into scikit-learn:main May 13, 2026
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KNeighborsClassifier fails on non-numeric labels with ValueError on certain parameters combination

4 participants