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

Incorrect number of predictions when using single record with naive bayes #345

Closed
pverspeelt opened this issue Jan 1, 2016 · 1 comment
Closed

Comments

@pverspeelt
Copy link

@pverspeelt pverspeelt commented Jan 1, 2016

I encountered a question on SO. I reproduced the encountered error in the code below.

When you use the naive bayes method with caret and then use predict on one record, you get more predictions returned than just one. Strangely enough it only happens with the formula notation, and only with prediction type = "raw". With type = "prob" it works correctly. I also tested it with different methods, but for now it only happens with hte naive bayes method. Directly using the KlaR package works as expected.

data(iris)
library(caret)

my_control <- trainControl(method = "cv")

set.seed(42)
nbfit_default <- train(x = iris[, -5],
                       y = iris$Species,
                       method="nb",
                       trControl = my_control)


set.seed(42)
nbfit_formula <- train(Species ~ ., 
                       data = iris, 
                       method = "nb", 
                       trControl = my_control)

single_value <- (iris[2, -5])

# predict returns 4 values
predict(nbfit_formula, single_value, type = "raw")

# switch to prob and one result returns
predict(nbfit_formula, single_value, type = "prob") # result OK

# no issue with the default calls
predict(nbfit_default, single_value, type = "raw") # result OK
predict(nbfit_default, single_value, type = "prob") # result OK


 #Predict with two records or more works fine 
multiple_values <- iris[1:2,]
predict(nbfit_formula, multiple_values, type = "raw") # result OK
predict(nbfit_formula, multiple_values, type = "prob") # result OK
predict(nbfit_default, multiple_values, type = "raw") # result OK
predict(nbfit_default, multiple_values, type = "prob") # result OK

# run model direct in KlaR, no issues
set.seed(2424)
nb_formula <- NaiveBayes(Species ~ ., data = iris)
nb_default <- NaiveBayes(x = iris[, -5],
                         y = iris$Species,
                         grouping = iris$Species)

predict(nb_formula, single_value) # result OK
predict(nb_default, single_value) # result OK
topepo added a commit that referenced this issue Jan 1, 2016
@topepo
Copy link
Owner

@topepo topepo commented Jan 1, 2016

The data were coming into the prediction function as a matrix and was getting converted to the wrong format. I've made a change to fix this.

topepo added a commit that referenced this issue Jan 1, 2016
@topepo topepo closed this Jan 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.