Skip to content

Ensure output of classifier predict has aligned index - #8056

Merged
rapids-bot[bot] merged 2 commits into
NVIDIA:mainfrom
jcrist:decode-labels-with-index
May 6, 2026
Merged

Ensure output of classifier predict has aligned index#8056
rapids-bot[bot] merged 2 commits into
NVIDIA:mainfrom
jcrist:decode-labels-with-index

Conversation

@jcrist

@jcrist jcrist commented May 6, 2026

Copy link
Copy Markdown
Member

When returning pandas/cudf outputs, we want the index of the output to be aligned with the index of the input. This PR plumbs through index in decode_labels, adds a test for all classifiers, then applies the necessary fixes and plumbing so the output has an aligned index.

In the long run I hope to move index alignment handling to the reflect decorator so method code doesn't need to worry about this, but for now this is the most idiomatic and cleanest solution.

Split out and expanded from #8039.

@jcrist jcrist self-assigned this May 6, 2026
@jcrist
jcrist requested a review from a team as a code owner May 6, 2026 16:47
@jcrist
jcrist requested a review from viclafargue May 6, 2026 16:47
@github-actions github-actions Bot added the Cython / Python Cython or Python issue label May 6, 2026
@jcrist jcrist added improvement Improvement / enhancement to an existing function non-breaking Non-breaking change labels May 6, 2026
@coderabbitai

coderabbitai Bot commented May 6, 2026

Copy link
Copy Markdown

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: 173b16b8-8849-49d1-b397-903643dc79cd

📥 Commits

Reviewing files that changed from the base of the PR and between 4c30f68 and a8f9394.

📒 Files selected for processing (10)
  • python/cuml/cuml/common/classification.py
  • python/cuml/cuml/ensemble/randomforestclassifier.py
  • python/cuml/cuml/fil/fil.pyx
  • python/cuml/cuml/linear_model/logistic_regression.py
  • python/cuml/cuml/linear_model/mbsgd_classifier.py
  • python/cuml/cuml/multiclass/multiclass.py
  • python/cuml/cuml/neighbors/kneighbors_classifier.pyx
  • python/cuml/cuml/svm/linear_svc.py
  • python/cuml/cuml/svm/svc.py
  • python/cuml/tests/test_base.py

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Classification model predictions now consistently preserve the index information when using pandas DataFrames as input across all classifiers, including RandomForest, LogisticRegression, KNeighborsClassifier, SVC, and LinearSVC.
    • Index metadata is properly maintained throughout the prediction pipeline for both single-target and multi-target classification scenarios.

Walkthrough

This PR enhances the classification prediction pipeline to preserve and propagate index metadata through prediction outputs. The decode_labels function now accepts an optional index parameter, and multiple classifiers are refactored to capture index information from intermediate results and pass it through to final outputs.

Changes

Index Propagation Through Classification Pipeline

Layer / File(s) Summary
API Enhancement
python/cuml/cuml/common/classification.py
decode_labels gains an optional index=None parameter and now wraps all output paths (multi-target, single-target numeric, and non-numeric) with CumlArray(..., index=index) or DataFrame index=index to preserve the provided index.
Core Infrastructure
python/cuml/cuml/fil/fil.pyx
ForestInference.predict returns CumlArray(data=result, index=proba.index) for classifier probabilities instead of a plain array, enabling index propagation upstream.
Multiclass Base Implementation
python/cuml/cuml/multiclass/multiclass.py
_BaseMulticlassClassifier.predict and decision_function now call check_inputs(return_index=True) to capture index and return CumlArray(data=out, index=index), establishing index propagation for multiclass estimators.
Classifier Predict Methods
python/cuml/cuml/ensemble/randomforestclassifier.py, python/cuml/cuml/linear_model/logistic_regression.py, python/cuml/cuml/linear_model/mbsgd_classifier.py, python/cuml/cuml/svm/linear_svc.py, python/cuml/cuml/svm/svc.py, python/cuml/cuml/neighbors/kneighbors_classifier.pyx
Each classifier's predict method is refactored to defer to_output("cupy") conversion, capture index from intermediate results (decision_function, predict_proba, or FIL output), and pass the captured index to decode_labels. SVC additionally updates predict_proba to preserve index throughout the probability computation path.
Test Coverage
python/cuml/tests/test_base.py
Added verification that predictions preserve the input DataFrame's index when using pandas output type, ensuring index propagation is correctly maintained end-to-end.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes


Possibly related PRs

  • rapidsai/cuml#8023: Modifies prediction output handling and index propagation in ForestInference and RandomForestClassifier, with decode_labels enhancements to preserve and return indexes alongside predictions.

Suggested labels

Cython / Python, improvement, non-breaking


Suggested reviewers

  • hcho3
  • dantegd
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Ensure output of classifier predict has aligned index' clearly and specifically describes the main objective of the changeset - ensuring that classifier predictions maintain aligned indices with input data.
Description check ✅ Passed The description directly relates to the changeset by explaining the purpose of plumbing index through decode_labels, adding tests, and applying fixes for index alignment in classifier predict outputs.
Docstring Coverage ✅ Passed Docstring coverage is 90.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

@csadorf csadorf left a comment

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.

I agree that we should try to lift some of the churn into the reflect decorator in the future.

LGTM!

@jcrist

jcrist commented May 6, 2026

Copy link
Copy Markdown
Member Author

/merge

@rapids-bot
rapids-bot Bot merged commit 37cec26 into NVIDIA:main May 6, 2026
96 checks passed
@coderabbitai coderabbitai Bot mentioned this pull request May 6, 2026
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 improvement Improvement / enhancement to an existing function non-breaking Non-breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants