如何从 SVM 预测中得到连续概率

How to get the continuous probability from SVM prediction

我训练了一个二进制 SVM 分类器并做出了如下预测:

classifier = svm(formula = type ~ .,
                 data = train,
                 type = 'C-classification',
                 kernel = 'polynomial')
y_pred = predict(classifier, newdata = test[1:57])

我正在训练的标签 (type) 是一个因素。本例中的预测 (y_pred) 也是一个因子列表。如何获得这些预测的 probability/logits 以便生成 ROC 曲线?

为了解决这个问题,在构造分类器和进行预测时都需要指定probability = TRUE

classifier = svm(formula = type ~ .,
                 data = train,
                 type = 'C-classification', 
                 probability=TRUE,
                 kernel = 'polynomial')
y_pred = predict(classifier, newdata = test[1:57], probability=TRUE)

然后attr()可以用来检索概率分数:

prob = as.data.frame(attr(y_pred, "probabilities”))