Skip to content

DOC: Clarify that support vectors in SVMs can be sparse - #34048

Merged
StefanieSenger merged 5 commits into
scikit-learn:mainfrom
david-cortes-intel:svm_docx
May 27, 2026
Merged

StefanieSenger merged 5 commits into
scikit-learn:mainfrom
david-cortes-intel:svm_docx

Conversation

@david-cortes-intel

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

What does this implement/fix? Explain your changes.

Non-linear SVMs will produce sparse support vectors when the inputs are sparse, but the docs say that they will be ndarray. This PR fixes the documentation to reflect the fact that sparse inputs produce sparse support vectors. Since the class is configurable (sparse array vs. sparse matrix) and there doesn't appear to be a consistent term to refer to both in the docs, I'm mentioning both sparse classes in the docs.

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Any other comments?

@AnneBeyer AnneBeyer left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for catching this, @david-cortes-intel! I double-checked it with this code snippet:

import numpy as np
import scipy.sparse as sp
from sklearn.svm import SVC, NuSVC, SVR, NuSVR

for model in [SVC, NuSVC, SVR, NuSVR]:
    X_dense = np.array([[0, 1], [1, 0], [1, 1], [0, 0]], dtype=float)
    y = [0, 0, 1, 1]
    svc_dense = model()
    svc_dense.fit(X_dense, y)
    print(f"Dense type: {type(svc_dense.support_vectors_)}")  # <class 'numpy.ndarray'>

    X_sparse = sp.csr_array(X_dense)
    svc_sparse = model()
    svc_sparse.fit(X_sparse, y)
    print(f"Sparse type: {type(svc_sparse.support_vectors_)}") # <class 'scipy.sparse._csr.csr_matrix'>

@david-cortes-intel

Copy link
Copy Markdown
Contributor Author

Actually I forgot to add the same info about the other attributes like coef_ and dual_coef_ which can also be sparse, will add more changes.

@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label May 19, 2026
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label May 20, 2026
Comment thread sklearn/svm/_classes.py
Weights assigned to the features (coefficients in the primal
problem). This is only available in the case of a linear kernel.

`coef_` is a readonly property derived from `dual_coef_` and

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I assume you didn't add "If X is sparse, these will also be sparse." here intentionally because it is already stated in dual_coef_ and support_vectors?

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.

Correct. Or do you think it should also repeat it here?

@StefanieSenger StefanieSenger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks @david-cortes-intel!

Comment thread sklearn/svm/_classes.py Resolved
Co-authored-by: Stefanie Senger <[email protected]>
Comment thread sklearn/svm/_classes.py Resolved
Comment thread sklearn/svm/_classes.py Resolved
@StefanieSenger

Copy link
Copy Markdown
Member

Nice, thanks for the updates, @david-cortes-intel. I'll merge. (I think it is fine to refer to dual_coef_ and support_vectors_ from coef_ and infer the type.)

@StefanieSenger
StefanieSenger merged commit ba70de6 into scikit-learn:main May 27, 2026
33 checks passed
@jeremiedbb jeremiedbb mentioned this pull request Jun 1, 2026
16 tasks done
jeremiedbb pushed a commit that referenced this pull request Jun 2, 2026
prady0t pushed a commit to prady0t/scikit-learn that referenced this pull request Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants