sklearn CalibratedClassifierCV和SVM出错(error with sklearn CalibratedClassifierCV and SVM)

我想使用sklearn的CalibratedClassifierCV与sklearn的SVC结合来预测多类(9类)预测问题。 但是,当我运行它时,我收到以下错误。 对于不同的模型(即RandomForestCalssifier),相同的代码不会运行。

kf = StratifiedShuffleSplit(y, n_iter=1, test_size=0.2) clf = svm.SVC(C=1,probability=True) sig_clf = CalibratedClassifierCV(clf, method="isotonic", cv=kf) sig_clf.fit(X, y) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/g/anaconda/lib/python2.7/site-packages/sklearn/calibration.py", line 166, in fit calibrated_classifier.fit(X[test], y[test]) File "/home/g/anaconda/lib/python2.7/site-packages/sklearn/calibration.py", line 309, in fit calibrator.fit(this_df, Y[:, k], sample_weight) IndexError: index 9 is out of bounds for axis 1 with size 9

I want to use sklearn's CalibratedClassifierCV in conjuction with sklearn's SVC to make predictions for a multiclass (9 classes) prediction problem. However when I run it, I get the following error. This same code will run no problem with a different model (i.e RandomForestCalssifier).

kf = StratifiedShuffleSplit(y, n_iter=1, test_size=0.2) clf = svm.SVC(C=1,probability=True) sig_clf = CalibratedClassifierCV(clf, method="isotonic", cv=kf) sig_clf.fit(X, y) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/g/anaconda/lib/python2.7/site-packages/sklearn/calibration.py", line 166, in fit calibrated_classifier.fit(X[test], y[test]) File "/home/g/anaconda/lib/python2.7/site-packages/sklearn/calibration.py", line 309, in fit calibrator.fit(this_df, Y[:, k], sample_weight) IndexError: index 9 is out of bounds for axis 1 with size 9

最满意答案

这是使用One-vs-One策略的SVC的问题,因此决策函数具有形状(n_samples, n_classes * (n_classes - 1) / 2) 。 可能的解决方法是CallibratedClassifierCV(OneVsRestClassifier(SVC())) 。 如果你想使用sigmoidal校准,你也可以做SVC(probability=True)而不使用CallibratedClassifierCV 。

我想我们应该修复SVC决策功能。

This is a problem of the SVC using a One-vs-One strategy, and therefore the decision function having shape (n_samples, n_classes * (n_classes - 1) / 2). A possible workaround would be do to CallibratedClassifierCV(OneVsRestClassifier(SVC())). If you want to use sigmoidal calibration, you can also do SVC(probability=True) and not use CallibratedClassifierCV.

We should fix the SVC decision function, I think.

更多推荐