Skip to content

Instantly share code, notes, and snippets.

@ajtulloch
Created November 3, 2013 17:37
Show Gist options
  • Save ajtulloch/7292723 to your computer and use it in GitHub Desktop.
Save ajtulloch/7292723 to your computer and use it in GitHub Desktop.
buildTreeAtLevel :: (Examples -> Double) -> PB.SplittingConstraints -> Int -> Examples -> DecisionTree
buildTreeAtLevel leafWeight splittingConstraints level examples =
if shouldSplit splittingConstraints level examples bestSplit
then Branch {
_feature=_splitFeature bestSplit
, _value=_splitValue bestSplit
, _left=recur $ V.takeWhile takePredicate orderedExamples
, _right=recur $ V.dropWhile takePredicate orderedExamples
}
else Leaf (leafWeight examples) where
-- candidate splits
candidates = V.map (findBestSplit examples) (getFeatures examples)
-- best candidate from all the features
bestSplit = V.maximumBy (compare `on` _averageGain) candidates
-- sort the examples at this branch by the best feature
orderedExamples = sortFeature examples (_splitFeature bestSplit)
-- left branch takes <, right branch takes >
takePredicate ex = features' ex ! _splitFeature bestSplit < _splitValue bestSplit
-- construct the next level of the tree
recur = buildTreeAtLevel leafWeight splittingConstraints (level + 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment