2
$\begingroup$

I'm working with decision trees in python's scikit learn. Unlike many use cases for this, I'm not so much interested in the accuracy of the classifier at this point so much as I am extracting the specific path a data point takes through the tree when I call .predict() on it. Has anyone done this before? I'd like to build a data frame containing ($X_{i}$, path$_{i}$) pairs for use in a down-stream analysis.

$\endgroup$
  • $\begingroup$ Not an answer, but I'm sure you've seen: scikit-learn.org/stable/modules/generated/… But I don't know how to turn the visualizer into a data frame for predictions. $\endgroup$ – nfmcclure Oct 15 '15 at 17:07
  • $\begingroup$ @nfmcclure Thanks! Yes, I saw that. I actually thought about writing python code for reverse engineering the paths based on that output, but it's about 250 pages, if you save it to a .txt file. I figure it would be too buggy an approach. $\endgroup$ – Kyle. Oct 15 '15 at 17:15
2
$\begingroup$

Looks like this is easier to do in R, using the rpart library in combination with the partykit library. I'd ideally like to find a way to do this in python, but here's the code, for anyone who is interested (taken from here):

pathpred <- function(object, ...){
    ## coerce to "party" object if necessary
    if(!inherits(object, "party")) object <- as.party(object)

    ## get standard predictions (response/prob) and collect in data frame
    rval <- data.frame(response = predict(object, type = "response", ...))
    rval$prob <- predict(object, type = "prob", ...)

    ## get rules for each node
    rls <- partykit:::.list.rules.party(object)

    ## get predicted node and select corresponding rule
    rval$rule <- rls[as.character(predict(object, type = "node", ...))]

    return(rval)
}

Illustration using the iris data and rpart():

library("rpart")
library("partykit")
rp <- rpart(Species ~ ., data = iris)
rp_pred <- pathpred(rp)
rp_pred[c(1, 51, 101), ]

Yielding,

       response prob.setosa prob.versicolor prob.virginica
 1       setosa  1.00000000      0.00000000     0.00000000
 51  versicolor  0.00000000      0.90740741     0.09259259
 101  virginica  0.00000000      0.02173913     0.97826087
                                           rule
 1                          Petal.Length < 2.45
 51   Petal.Length >= 2.45 & Petal.Width < 1.75
 101 Petal.Length >= 2.45 & Petal.Width >= 1.75

Which looks to be something I could at least use to derive shared parent node information.

$\endgroup$
  • $\begingroup$ Accepting this answer for now, but if someone comes up with a python solution, I'll likely accept that instead. $\endgroup$ – Kyle. Oct 17 '15 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.