Skip to content

SVM dual_coef_ is forcefully set as sparse but would never have zeros #35600

Backlog

Description

@david-cortes-intel

Warning

This issue is not yet ready for a PR. If you are interested in contributing to scikit-learn, please have a look at our contributing guidelines, and in particular the sections for new contributors and the "Needs triage" label.

Introduce yourself

No response

Describe the bug and give evidence about its user-facing impact

In SVM classes, when the input is sparse, the support vectors (attribute support_vectors_) will also be sparse, since they are taken from the same data.

These support vectors have an associated coefficient in the dual problem formulation, kept in attribute dual_coef_. By definition, a support vector would have to be a point with an impact in the decision function, hence entries in dual_coef_ should be non-zeros, or otherwise the point would not be a support vector.

The underlying LibSVM that scikit-learn uses produces dual_coef_ as a dense array, but scikit-learn will forcefully cast that into a sparse format when the support vectors are sparse and then extract the .data attribute when passing that data to LibSVM, which doesn't sound logical.

There was some discussion around this topic when the code was first introduced:
#576 (comment)

But it was never addressed in that or the follow-up PRs.

Steps/Code to Reproduce

from sklearn.datasets import make_classification
X, y = make_classification(n_samples=20, n_features=3, n_redundant=0, random_state=123)

from scipy.sparse import csr_array
X = csr_array(X)

from sklearn.svm import SVC
model = SVC().fit(X,y)
model.dual_coef_.__class__

Expected Results

dual_coef_ should be a numpy array.

Actual Results

dual_coef_ is a CSR array.

Versions

Not relevant.

Interest in fixing the bug

No.

Activity

  1. Duke2nuke commented on Dec 2, 2026

    @Duke2nuke

    Hi, is this issue still free? I would like to work on it if so. My read is the fix is to stop forcing dual_coef_ into a sparse format when the support vectors are sparse and keep it as the dense array LibSVM returns, then add a small test with a sparse input to check the type. Please let me know if that direction sounds right.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds Triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions