How to get the continuous probability from SVM prediction

Viewed 164

I have trained a binary SVM classifier and made predictions like the following:

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

The label that I am training against (type) is a factor. The prediction (y_pred) in this case is also a factor list. How can I obtain the probability/logits of these predictions so that I can produce a ROC curve?

1 Answers

To solve this problem, probability = TRUE need to be specified both when constructing the classifier and making predictions:

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

Then the attr() can be used to retrieve the probability scores:

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