Created
November 3, 2013 17:37
-
-
Save ajtulloch/7292723 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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