Skip to content

Fix multiclass SVC with non-uniform class weights - #8592

Open
viclafargue wants to merge 2 commits into
NVIDIA:mainfrom
viclafargue:fix-svc-multiclass-class-weight
Open

Fix multiclass SVC with non-uniform class weights#8592
viclafargue wants to merge 2 commits into
NVIDIA:mainfrom
viclafargue:fix-svc-multiclass-class-weight

Conversation

@viclafargue

Copy link
Copy Markdown
Contributor

Fixes #8578.

Multiclass SVC converted class weights into per-sample weights but did not pass them to the binary classifiers. This caused incorrect predictions with non-uniform class weights.

This PR explicitly forwards the correct weights:

  • One-vs-one classifiers receive weights for their selected class pair.
  • One-vs-rest classifiers receive the complete weight array.
  • Binary classifiers no longer reinterpret the original class-weight mapping.

Regression tests cover class weights, sample weights, dense and sparse inputs, numeric and string labels, and both multiclass strategies.

@copy-pr-bot

copy-pr-bot Bot commented Sep 9, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@github-actions github-actions Bot added the Cython / Python Cython or Python issue label Sep 9, 2026
Comment on lines +31 to +36
mask = (y == class_i) | (y == class_j)
indices = np.flatnonzero(mask)
X_binary = X[indices]
if pairwise:
X_binary = X_binary[:, indices]
y_binary = (y[indices] == class_j).astype(np.int32)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Following Scikit-Learn's logic over here.

@viclafargue viclafargue added bug Something isn't working non-breaking Non-breaking change labels Sep 11, 2026
@viclafargue
viclafargue marked this pull request as ready for review September 11, 2026 08:16
@viclafargue
viclafargue requested a review from a team as a code owner September 11, 2026 08:16
@viclafargue
viclafargue requested a review from dantegd September 11, 2026 08:16
@coderabbitai

coderabbitai Bot commented Sep 11, 2026

Copy link
Copy Markdown

Review Change StackReview 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: 6eef85b2-d38e-4589-8ce4-9884fc4cd67a

📥 Commits

Reviewing files that changed from the base of the PR and between 1c3db4b and a274f69.

📒 Files selected for processing (3)
  • python/cuml/cuml/multiclass/multiclass.py
  • python/cuml/cuml/svm/svc.py
  • python/cuml/tests/test_svm.py

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


📝 Summary

Summary by CodeRabbit

  • New Features

    • Multiclass SVC models now support per-sample weighting during training.
    • Weighted One-vs-One and One-vs-Rest classification is supported for dense and sparse inputs.
    • Class labels can be numeric or string values when using custom or balanced class weights.
  • Bug Fixes

    • Improved handling of class and sample weights to prevent weights from being applied more than once.
  • Documentation

    • Updated multiclass classifier documentation to describe weighted fitting behavior.

Walkthrough

The change adds weighted One-vs-One and One-vs-Rest fitting, propagates weights through multiclass SVC, prevents class-weight duplication, preserves feature metadata, and adds comparisons with scikit-learn.

Changes

Weighted multiclass SVC

Layer / File(s) Summary
Weighted multiclass fitting
python/cuml/cuml/multiclass/multiclass.py
_BaseMulticlassClassifier.fit accepts sample_weight. Weighted OvO and OvR fitting trains cloned binary estimators with validated weights and preserves feature metadata. Documentation describes the cuML fitting path.
SVC weight wiring
python/cuml/cuml/svm/svc.py
Multiclass SVC passes processed sample weights to the wrapper and clears binary class_weight after class weights are incorporated into the sample weights.
Multiclass SVC validation
python/cuml/tests/test_svm.py
Tests cover custom, per-sample, and balanced class weights across dense and sparse inputs, label types, and OvO/OvR decision functions.

Priority: ➖ Normal

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to e3dd1

Weighted multiclass SVC fitting now propagates weights through the binary estimators and is validated against scikit-learn across the stated strategy and input variants. No merge-blocking risk remains.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 30.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 10 functions across 3 files. 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 identifies the main change: fixing multiclass SVC behavior with non-uniform class weights.
Description check ✅ Passed The description directly explains the class-weight propagation issue, the implemented fix, and the regression test coverage.
Linked Issues check ✅ Passed The change meets the coding requirements in issue #8578. SVC._fit_multiclass forwards processed sample weights and disables binary class_weight reuse. The weighted OvO helper selects each class pa…
Out of Scope Changes check ✅ Passed The changes stay within issue #8578. The generic multiclass fitting helpers are required to propagate pair-specific and complete weights. The sample_weight support and documentation updates support …
✨ 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

bug Something isn't working Cython / Python Cython or Python issue non-breaking Non-breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Multiclass SVC produces incorrect predictions with non-uniform class_weight

2 participants