Skip to content

Comments

Fix sparse SVR empty support vectors (swev-id: scikit-learn__scikit-learn-14894)#48

Open
casey-brooks wants to merge 1 commit intoscikit-learn__scikit-learn-14894from
noa/issue-45
Open

Fix sparse SVR empty support vectors (swev-id: scikit-learn__scikit-learn-14894)#48
casey-brooks wants to merge 1 commit intoscikit-learn__scikit-learn-14894from
noa/issue-45

Conversation

@casey-brooks
Copy link

Bug

BaseLibSVM._sparse_fit divides by n_class when building the CSR pointer array. When libsvm_sparse returns no support vectors for sparse SVR/NuSVR inputs the dual coefficient buffer is empty, so the pointer step is zero and NumPy raises a ZeroDivisionError.

Reference: #45

Reproduction

  1. Create the sparse dataset from the issue.
  2. Fit SVR(kernel='linear', gamma=1.0, C=316.227766017, epsilon=0.1) on dense data (succeeds).
  3. Fit the same estimator on the CSR copy of the data.
Traceback (most recent call last):
  File "<stdin>", line 13, in <module>
  File "/workspace/scikit-learn/sklearn/svm/base.py", line 198, in fit
    fit(X, y, sample_weight, solver_type, kernel, random_seed=seed)
  File "/workspace/scikit-learn/sklearn/svm/base.py", line 291, in _sparse_fit
    dual_coef_indices.size / n_class)
ZeroDivisionError: float division by zero

Fix

  • guard _sparse_fit so we build an empty CSR dual coefficient matrix when no support vectors are returned
  • add regression coverage that exercises sparse SVR with zero support vectors
  • tidy a nearby long ValueError string to keep flake8 happy

Tests

  • Added test_sparse_svr_empty_support_vectors
  • pytest sklearn/svm/tests/test_sparse.py
  • flake8 sklearn/svm/base.py sklearn/svm/tests/test_sparse.py

@casey-brooks casey-brooks requested a review from a team December 26, 2025 04:04
@casey-brooks
Copy link
Author

Local validation summary:

  • pytest sklearn/svm/tests/test_sparse.py (32 passed, 0 failed, 0 skipped)
  • flake8 sklearn/svm/base.py sklearn/svm/tests/test_sparse.py (no issues)

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

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

Guard avoids the ZeroDivisionError and the regression test covers the sparse SVR case. Thanks!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants