Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return probability with selfTraining and SVM #2

Open
GregStacey opened this issue Jan 12, 2021 · 3 comments
Open

Return probability with selfTraining and SVM #2

GregStacey opened this issue Jan 12, 2021 · 3 comments

Comments

@GregStacey
Copy link

I'm trying to return the class probabilities as predicted by selfTraining and SVM. The code below is from the selfTraining documentation. It seems like it should be returning probability but is not. Any help?

## Load Wine data set
data(wine)
cls <- which(colnames(wine) == "Wine")
x <- wine[, -cls] # instances without classes
y <- wine[, cls] # the classes
x <- scale(x) # scale the attributes
## Prepare data
set.seed(20)
# Use 50% of instances for training
tra.idx <- sample(x = length(y), size = ceiling(length(y) * 0.5))
xtrain <- x[tra.idx,] # training instances
ytrain <- y[tra.idx]  # classes of training instances
# Use 70% of train instances as unlabeled set
tra.na.idx <- sample(x = length(tra.idx), size = ceiling(length(tra.idx) * 0.7))
ytrain[tra.na.idx] <- NA # remove class information of unlabeled instances
# Use the other 50% of instances for inductive testing
tst.idx <- setdiff(1:length(y), tra.idx)
xitest <- x[tst.idx,] # testing instances
yitest <- y[tst.idx] # classes of testing instances
## Example: Training from a set of instances with 1-NN as base classifier.
learner <- e1071::svm
learner.pars <- list(type = "C-classification", kernel="radial", 
                     probability = TRUE, scale = TRUE)
pred <- function(m, x){
  r <- predict(m, x, probability = TRUE)
  prob <- attr(r, "probabilities")
  prob
}
m3 <- selfTraining(x = xtrain, y = ytrain, 
                   learner = learner, 
                   learner.pars = learner.pars, 
                   pred = pred)
pred3 <- predict(m3, xitest, probability=TRUE)
head(attr(pred3, "probabilities"))
@cbergmeir
Copy link
Collaborator

Hi, I just had a look and regretfully that's not how it is implemented though it would be easy to do. As a workaround, you should be able to get probabilities in your example with:

object <- m3
x <- xitest

result <- ssc:::checkProb(
      ssc:::predProb(object$model, x, object$pred, object$pred.pars), 
      ninstances = nrow(x), 
      object$classes
    )

  
result

@osmanirosado
Copy link
Collaborator

osmanirosado commented Feb 5, 2021

Using the example of @cbergmeir , you can fix your your code as follows

pred3 <- ssc:::predProb(m3$model, x, m3$pred, m3$pred.pars)
head(pred3)

Note that the predict function for the selfTraining class does not have a probability argument,
see the implementation of that function here.

@cbergmeir
Copy link
Collaborator

Hi Osmani, we could easily implement it though with something like the following in predict.selfTraining:

result <- checkProb(
predProb(object$model, x, object$pred, object$pred.pars),
ninstances = nrow(x),
object$classes
)

if (!probability) result <- getClass(result)

return(result)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants