1

I'm creating rules from the decision tree generated by mlr package wrapper learner classif.rpart, how to print the rules as in rpart.rules and how to visualize as in rpart.plot

Created the learner using classif.rpart, trained and fitted the model, tried to plot using rpart.plot and saying error as Not an rpart object

dt_mod <- mlr::train(fused_dt, classif.task)
dt_mod
library(rpart.plot)
rpart.plot(dt_mod$learner.model, roundint=FALSE, varlen=3, type = 3, clip.right.labs = FALSE, yesno = 2)
rpart.rules(dt_mod$learner.model, roundint = FALSE)

I except the rules to be listed and tree to be visualized

Pablo Ezequiel Inchausti
  • 16,623
  • 7
  • 28
  • 42
hanzgs
  • 1,498
  • 17
  • 44
  • The learner is made with makeFilterWrapper, with fw.method, then parameter tuning is done, will that model considered as rpart object? – hanzgs Feb 13 '19 at 02:46

1 Answers1

3

You can access the model a learner has induced directly using getLearnerModel():

iris.model = train(makeLearner("classif.rpart"), iris.task)
rpart.plot(getLearnerModel(iris.model))

If your learner is wrapped, pass the more.unwrap = TRUE option:

rpart.plot(getLearnerModel(iris.model, more.unwrap = TRUE))
Lars Kotthoff
  • 107,425
  • 16
  • 204
  • 204
  • wow, thats really great, i have been searching this for very long time and in many forums,great, Thank you very much, may i know any documentation for this in mlr available please, Thanks – hanzgs Feb 14 '19 at 02:54
  • Sure, `help("getLearnerModel")`. – Lars Kotthoff Feb 14 '19 at 04:50
  • You can set the hyperparameters of the model to do pruning as it is trained. Is that what you mean? – Lars Kotthoff Feb 21 '19 at 16:29
  • Yes Many Thanks, thats what i liked to confirm. – hanzgs Feb 21 '19 at 23:12
  • I am working on printing Tree Rules, if I use classif.extraTrees (Extremely Randomized Trees), using getTree(extratree_model, 1, labelVar=TRUE), it is working for randomForest, not for this extraTrees, is there any other option? – hanzgs Feb 25 '19 at 03:04
  • Also getLearnerModel(extratrees_mod, more.unwrap = TRUE) is printing rules on j48 but not on this. – hanzgs Feb 25 '19 at 03:05
  • https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3399093/ , say 50 decision trees have been used to construct each Extra-Trees model, they cannot directly be translated into simple rules. Additional work is required to simplify these models to make them accessible and understandable by bedside clinicians. – hanzgs Feb 25 '19 at 03:32
  • For printing the actual models, mlr doesn't do anything extra -- if the underlying model doesn't support printing, you can't print it. – Lars Kotthoff Feb 25 '19 at 16:16
  • I got it, For mlr RandomForest this link has a function for printing rules and plotting any tree in the forest, https://www.google.com.au/url?sa=t&rct=j&q=&esrc=s&source=web&cd=11&cad=rja&uact=8&ved=2ahUKEwjLxOrQhdjgAhVOU30KHfEKAtQQFjAKegQICBAB&url=https%3A%2F%2Fwww.researchgate.net%2Ffile.PostFileLoader.html%3Fid%3D57a9cc69f7b67ef807028052%26assetKey%3DAS%253A393150137946112%25401470745705122&usg=AOvVaw2_3q6TfS5_tGh2uZMeRWiI That work good for most the tree based models in mlr, hope you may be knowing that, just FYKI, Thanks – hanzgs Feb 25 '19 at 23:35
  • Hi Lars, regarding normalizeFeatures in mlr, after printing rules, say i get a below rule user_sal > -1.09079588004389 & User_purpose < 0.12893 & User_apn > 0.32324865 => Bad May i know how to intrepret to original values please, Thanks – hanzgs Feb 26 '19 at 01:50
  • That depends on how exactly you're doing the normalization. In general, if you want to interpret the rules, don't normalize. – Lars Kotthoff Feb 26 '19 at 01:59
  • Hi Lars, I find issues in with wrapper models in mlr, when I use unwrap ``` getLearnerModel(iris.model, more.unwrap = TRUE) ``` I get only prediction as 0/1, if I use predict.type as probability I am not getting percentages in production environments. For Deployment in Production,i have to convert to PMML or PFA it is not accepting the wrapper, also in AzureML it is nor working for wrapper mlr models, is there a way to use as it is without unwrap, Thanks – hanzgs Apr 04 '19 at 02:07