Skip to content

Return Euclidean distances from KMeans transform - #8539

Open
Hashim1999164 wants to merge 1 commit into
NVIDIA:mainfrom
Hashim1999164:fix/kmeans-transform-euclidean
Open

Return Euclidean distances from KMeans transform#8539
Hashim1999164 wants to merge 1 commit into
NVIDIA:mainfrom
Hashim1999164:fix/kmeans-transform-euclidean

Conversation

@Hashim1999164

@Hashim1999164 Hashim1999164 commented Sep 1, 2026

Copy link
Copy Markdown

Summary

KMeans.transform currently returns squared distances. sklearn returns Euclidean distances.

This change takes the square root of the transform output so the Python API matches sklearn. score is unchanged.

Fixes #8536

Test plan

Added a regression test against the six point example from the issue.

@Hashim1999164
Hashim1999164 requested a review from a team as a code owner September 1, 2026 16:14
@copy-pr-bot

copy-pr-bot Bot commented Sep 1, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions github-actions Bot added the Cython / Python Cython or Python issue label Sep 1, 2026
@coderabbitai

coderabbitai Bot commented Sep 1, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 3976ec8a-030f-4358-bcb7-d3176a86d5e5

📥 Commits

Reviewing files that changed from the base of the PR and between 067c1e3 and c1afc7b.

📒 Files selected for processing (2)
  • python/cuml/cuml/cluster/kmeans.pyx
  • python/cuml/tests/test_kmeans.py

Included review availability: Your plan provides up to 12 included reviews per hour; 11 remain after this review.


📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Corrected KMeans transformations to return Euclidean distances instead of squared-Euclidean distances.
    • Improved consistency with scikit-learn distance results.
  • Tests

    • Added regression coverage validating calculated KMeans distances against expected values and scikit-learn.

Walkthrough

KMeans.transform now returns Euclidean distances instead of squared Euclidean distances. A regression test validates explicit expected values and scikit-learn compatibility.

Changes

KMeans distance correction

Layer / File(s) Summary
Distance conversion and regression coverage
python/cuml/cuml/cluster/kmeans.pyx, python/cuml/tests/test_kmeans.py
KMeans.transform applies an elementwise square root to backend squared distances. The regression test checks fixed expected distances and scikit-learn results.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to c1afc

KMeans.transform now returns Euclidean distances to match the documented sklearn-compatible behavior, with regression coverage added. No actionable merge-blocking risk remains beyond normal checks and review.

Suggested reviewers: betatim

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 1 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely states the primary change: KMeans.transform now returns Euclidean distances.
Description check ✅ Passed The description accurately explains the squared-distance bug, the scikit-learn compatibility goal, the unchanged score behavior, and the regression test.
Linked Issues check ✅ Passed The implementation applies a square root to KMeans.transform output, and the added test checks explicit expected values and scikit-learn results. These changes satisfy issue #8536.
Out of Scope Changes check ✅ Passed The changes are limited to the KMeans.transform fix and its regression test. No unrelated code or scope was identified.
Full details: Docstring Coverage

Explanation

Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 1 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Cython / Python Cython or Python issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] KMeans.transform returns squared distances instead of Euclidean distances

2 participants