Hi Rafael When the tree needs to make a prediction, it usually goes through the predict method, then the apply method, then the _apply_dense method (this helps partition between dense and sparse data).
Take a look at lines 3463 to 3503, the _apply_dense method. This ends up returning an array of offsets to the predict method, where each offset is the leaf node a point falls under. The predict method then indexes the value array (where node prediction values) are stored by this offset array, assigning a prediction value to each point. A small source of confusion is that for regression trees, the value array is one value per output per node, which makes sense. However, for classification trees, the value array stores the number of training points for each class for each output for each node. For example, a regression tree may have 2.5 as the prediction value in a leaf, but a classification tree may have [3, 40, 5] as the value in a leaf if there are three classes. The final prediction uses argmax to select class 1. Let me know if you have any other questions! Jacob On Tue, Aug 11, 2015 at 2:17 PM, Rafael Calsaverini < rafael.calsaver...@gmail.com> wrote: > Hi there all, > > I'm taking a look on the code for decision trees and trying to understand > how it actually decides the class and I'm having some trouble with the > final step. > > The heart of the algorithm seem to be on lines 3249 to 3260 of > the sklearn/tree/_tree.pyx file. > > Lines 3249 to 3258 are fine, they are just the standard walking through > the branchs on the decision trees. What I failed to understand is how the > tree actually decides which class to assign to the sample being classified > after it reaches a leaf node. Aren't the final classes assigned to each > final branch stored anywhere? > > Thanks, > Rafael Calsaverini > > > > > > > ------------------------------------------------------------------------------ > > _______________________________________________ > Scikit-learn-general mailing list > Scikit-learn-general@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/scikit-learn-general > >
------------------------------------------------------------------------------
_______________________________________________ Scikit-learn-general mailing list Scikit-learn-general@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/scikit-learn-general