Created
November 3, 2013 21:37
-
-
Save ajtulloch/7295132 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 leftExamples | |
, _right=recur rightExamples | |
} | |
else Leaf {_value=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 > | |
branchLeft ex = | |
(features' ex ! _splitFeature bestSplit) < | |
_splitValue bestSplit | |
-- construct the next level of the tree | |
recur = | |
buildTreeAtLevel leafWeight splittingConstraints (level + 1) | |
leftExamples = V.takeWhile branchLeft orderedExamples | |
rightExamples = V.dropWhile branchLeft orderedExamples |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment