我需要一个 SVM 作为多标签分类器,所以我决定使用 OneVsRestClassifier 包装器。然而问题出现了,训练集变得高度不平衡:对于给定的类,负样本比正样本多得多。这可以通过 class_weight 参数来解决,但如果我在封装在 OneVsRestClassifier 中的分类器中使用它,则会出现错误:
from sklearn.svm import LinearSVC
from sklearn.multiclass import OneVsRestClassifier
weights = {'ham': 1, 'eggs': 2}
svm = OneVsRestClassifier(LinearSVC(class_weight=weights))
X = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]
Y = [['ham'], [], ['eggs', 'spam'], ['spam'], ['eggs']]
svm.fit(X, Y)
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python2.7/site-packages/sklearn/multiclass.py", line 197, in fit
n_jobs=self.n_jobs)
File "/usr/local/lib/python2.7/site-packages/sklearn/multiclass.py", line 87, in fit_ovr
for i in range(Y.shape[1]))
File "/usr/local/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 514, in __call__
self.dispatch(function, args, kwargs)
File "/usr/local/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 311, in dispatch
job = ImmediateApply(func, args, kwargs)
File "/usr/local/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 135, in __init__
self.results = func(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/sklearn/multiclass.py", line 56, in _fit_binary
estimator.fit(X, y)
File "/usr/local/lib/python2.7/site-packages/sklearn/svm/base.py", line 681, in fit
self.classes_, y)
File "/usr/local/lib/python2.7/site-packages/sklearn/utils/class_weight.py", line 49, in compute_class_weight
if classes[i] != c:
IndexError: index 2 is out of bounds for axis 0 with size 2