Exemple #1
0
        public static DecisionTree BuildFromDataSet(DataSetObject[] dataSet, int maxHeight)
        {
            var headNodeDecision = DecisionTreeItem.GetBestDecisionFromDataSet(dataSet, out NodeSplit split);
            var headNode         = InitializeTreeItem(headNodeDecision, dataSet, split, maxHeight);

            return(new DecisionTree(headNode));
        }
Exemple #2
0
        private static DecisionTreeItem InitializeTreeItem(Decision decision, IReadOnlyCollection <DataSetObject> dataSet, NodeSplit curSplit, int levelAvailableCount)
        {
            var label = dataSet.First().Label;

            if (dataSet.All(k => k.Label == label))
            {
                return(new DecisionTreeLeaf(dataSet.First().Label));
            }

            if (levelAvailableCount == 1)
            {
                return(new DecisionTreeLeaf(dataSet.GetClassByMaxCount()));
            }

            levelAvailableCount--;

            var leftDecision = DecisionTreeItem.GetBestDecisionFromDataSet(curSplit.LeftPart, out var leftSplit);
            var leftChild    = InitializeTreeItem(leftDecision, curSplit.LeftPart, leftSplit, levelAvailableCount);

            var rightDecision = DecisionTreeItem.GetBestDecisionFromDataSet(curSplit.RightPart, out var rightSplit);
            var rightChild    = InitializeTreeItem(rightDecision, curSplit.RightPart, rightSplit, levelAvailableCount);

            return(new DecisionTreeNode(leftChild, rightChild, decision, dataSet.GetClassByMaxCount()));
        }