scikit-learn: NuSVC argument `class_weight` is not used
Describe the bug
Like SVC, the class NuSVC takes argument class_weight. However, it looks like this argument is not used. After a quick look at the libsvm C code within sklearn as well as libsvm’s original documentation, this seems to be expected: “wi set the parameter C of class i to weight*C, for C-SVC”. I suggest that this argument should be removed from NuSVC’s constructor and from the documentation.
Steps/Code to Reproduce
from sklearn.svm import SVC, NuSVC
X = [[1., 2, 3], [0, 5, 2]]
y = [-1, 1]
NuSVC(verbose=True).fit(X, y).dual_coef_
optimization finished, #iter = 0
C = 2.587063
obj = 1.293532, rho = 0.000000
nSV = 2, nBSV = 0
Total nSV = 2
Out: [LibSVM]array([[-1.29353162, 1.29353162]])
SVC(C=2.587063, verbose=True).fit(X, y).dual_coef_
optimization finished, #iter = 1
obj = -1.293532, rho = 0.000000
nSV = 2, nBSV = 0
Total nSV = 2
Out: [LibSVM]array([[-1.29353162, 1.29353162]])
NuSVC(class_weight={-1:1.5, 1:.2}, verbose=True).fit(X, y).dual_coef_
optimization finished, #iter = 0
C = 2.587063
obj = 1.293532, rho = 0.000000
nSV = 2, nBSV = 0
Total nSV = 2
Out: [LibSVM]array([[-1.29353162, 1.29353162]])
SVC(C=2.587063, class_weight={-1:1.5, 1:.2}, verbose=True).fit(X, y).dual_coef_
optimization finished, #iter = 1
obj = -0.827860, rho = -0.600000
nSV = 2, nBSV = 1
Total nSV = 2
Out: [LibSVM]array([[-0.5174126, 0.5174126]])
NuSVC(class_weight={-1:0, 1:0}).fit(X, y).dual_coef_
Out: array([[-1.29353162, 1.29353162]])
SVC(class_weight={-1:0, 1:0}).fit(X, y).dual_coef_
Out: array([], shape=(1, 0), dtype=float64)
Expected Results
As in the case of no class_weight, NuSVM should give the same dual_coef_ as an SVC with the same C.
Also class_weight={-1:0, 1:0} should give the “empty” result.
Actual Results
In all cases above NuSVM with class weight behaves exactly as when no weights are given.
Versions
System:
python: 3.9.16 | packaged by conda-forge | (main, Feb 1 2023, 21:39:03) [GCC 11.3.0]
executable: .../bin/python3.9
machine: Linux-6.8.0-48-generic-x86_64-with-glibc2.39
Python dependencies:
sklearn: 1.5.2
pip: 23.0.1
setuptools: 67.6.0
numpy: 2.0.2
scipy: 1.13.1
Cython: None
pandas: None
matplotlib: 3.9.2
joblib: 1.4.2
threadpoolctl: 3.5.0
Built with OpenMP: True
threadpoolctl info:
user_api: blas
internal_api: blis
num_threads: 1
prefix: libblis
filepath: .../lib/libblis.so.4.0.0
version: 0.9.0
threading_layer: pthreads
architecture: skx
user_api: openmp
internal_api: openmp
num_threads: 8
prefix: libgomp
filepath: .../lib/libgomp.so.1.0.0
version: None
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 1
- Comments: 15 (9 by maintainers)
Yes the part of my code snippet above that uses
sample_weightworks well enough. We just need to fix theclass_weighttosample_weightconversion as done in #30366 to have the second part of my code snippet assertions to pass as well.Absolutely. Thanks!
@gdacciaro That doc is actually incorrect (so we may want to fix that too). It says that
class_weightis passed tofitand is a dictionary. However the similarly named argument that is passed tofitissample_weightand it’s a per-sample version of the weights (and it’s a vector, not a dictionary). The line you mention should read “SVC(but not :class:NuSVC) implements the parameterclass_weightin the constructor”.