classification - R - factor examcard has new levels -


i built classification model in r using c5.0 given below:

library(c50) library(caret) = read.csv("all_srn.csv") set.seed(123) intrain <- createdatapartition(a$anatomy, p = .70, list = false) training <- a[ intrain,] test <- a[-intrain,] tree <- c5.0(anatomy ~ ., data = training,              trcontrol = traincontrol(method = "repeatedcv", repeats = 10,                                      classprob = true)) treepred <- predict(tree, test) 

the training set has features - examcard, coil_used, anatomy_region, bodypart_anatomy , anatomy(target class). features categorical variables. there total of 10k odd values, divided data training , test data. learner worked great training , test set partioned in 70:30 ratio, problem comes when provide test set new values given below:

treepred <- predict(tree, test_add) 

here, test_add contains present test set , set of new values , on executing learner fails classify new values , throws following error:

error in model.frame.default(object$terms, newdata, na.action = na.action, : factor examcard has new levels 

i tried merge new factor levels existing 1 using:

tree$xlevels[["examcard"]] <- union(tree$xlevels[["examcard"]], levels(test_add$examcard)) 

but, wasn't of since code executed following message , didn't yield fruitful result:

predict code called exit value 1 

the feaure examcard holds deal of primacy in classification hence can't ignored. how can these set of values classified?

you cannot create prediction factor levels in test set absent in training set. model not have coefficients these new factor levels.

if doing 70/30 split, need repartition data using caret::createdatapartition...

... or own stratified sample function ensure levels represented in training set: use "split-apply-combine" approach: split data set examcard, , each subset, apply split, combine training subsets , testing subsets.

see this question more details.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -