示例#1
0
        private string predict(DataRow dataRow, DecisionTreeNode root)
        {
            if (dataRow == null)
            {
                throw new ArgumentNullException(nameof(dataRow));
            }
            if (!IsTrained)
            {
                throw new InvalidOperationException("DecisionTree cannot predict without being trained");
            }

            /// If this node is a leaf node, then we have a successful prediction.
            if (root.IsLeaf)
            {
                return(root.ClassLabel);
            }

            /// Get the split dimension's value and see if first child has that
            /// value, if it does, then recurse into the first child to predict.
            var value = dataRow.GetValueAtIndex(root.SplitDimensionIndex);

            if (root.HasFirstChild(value))
            {
                return(predict(dataRow, root.FirstChild));
            }
            /// If the first child's value doesn't match, then see if second child
            /// exists. If it does, then recurse into second child to predict.
            else if (root.SecondChild != null)
            {
                return(predict(dataRow, root.SecondChild));
            }
            /// If the seecond doesn't exist, then predict the most frequest class label.
            return(mostFrequentClassLabel);
        }