Repository navigation
DOC: Clarify that support vectors in SVMs can be sparse - #34048
Conversation
There was a problem hiding this comment.
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'>|
Actually I forgot to add the same info about the other attributes like |
cae6c09 to
c77bcce
Compare
| 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Correct. Or do you think it should also repeat it here?
StefanieSenger
left a comment
There was a problem hiding this comment.
LGTM, thanks @david-cortes-intel!
Co-authored-by: Stefanie Senger <[email protected]>
|
Nice, thanks for the updates, @david-cortes-intel. I'll merge. (I think it is fine to refer to |
Co-authored-by: Stefanie Senger <[email protected]>
…#34048) Co-authored-by: Stefanie Senger <[email protected]>
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:
Any other comments?