private void BuildConditionalNodesRecursive(IList<IDataRow> rows, IList<string> attributes, ConditionalTree.ConditionalNode parentNode) { var attributesList = attributes.ToList(); if (attributesList.Count == 0) { return; } bool sameClass; var result = ComputeBestGain(rows, attributes, out sameClass); if (sameClass) { parentNode.Class = rows.First().Class; return; } var attributeResult = result.Item2; var first = true; var children = new List<ConditionalTree.ConditionalNode>(); attributesList.Remove(attributeResult.Attribute); foreach (var subset in attributeResult.Subsets) { var node = new ConditionalTree.ConditionalNode { Condition = attributeResult.IsNumeric ? first ? ConditionalTree.PredicateCondition.LessThanOrEqual : ConditionalTree.PredicateCondition.GreaterThan : ConditionalTree.PredicateCondition.Equal, ThreshHold = subset.Value, Attribute = attributeResult.Attribute }; BuildConditionalNodesRecursive(subset.Rows, attributesList, node); first = false; children.Add(node); } parentNode.Children = children; }
private void BuildConditionalNodes(IList<int> rows, IList<int> attributesIndexes, ConditionalTree.ConditionalNode parentNode) { var stack = new Stack<Iteration>(); stack.Push(new Iteration { Attributes = attributesIndexes, ParentNode = parentNode, Rows = rows, }); Iteration currentIteration = stack.Pop(); while (currentIteration != null) { var attributesList = currentIteration.Attributes.ToList(); if (attributesList.Count == 0) { currentIteration = stack.Count > 0 ? stack.Pop() : null; continue; } bool sameClass; var result = ComputeBestGain(currentIteration.Rows, currentIteration.Attributes, out sameClass); if (sameClass) { currentIteration.ParentNode.Class = _data.ClassesValue[_data.Class(currentIteration.Rows[0])]; currentIteration = stack.Count > 0 ? stack.Pop() : null; continue; } var attributeResult = result.Item2; var first = true; var children = new List<ConditionalTree.ConditionalNode>(); attributesList.Remove(attributeResult.AttributeIndex); foreach (var subset in attributeResult.Subsets) { var node = new ConditionalTree.ConditionalNode { Condition = attributeResult.IsNumeric ? first ? ConditionalTree.PredicateCondition.LessThanOrEqual : ConditionalTree.PredicateCondition.GreaterThan : ConditionalTree.PredicateCondition.Equal, ThreshHold = subset.Value, Attribute = _data.Attributes[attributeResult.AttributeIndex] }; stack.Push(new Iteration { Rows = subset.Rows, Attributes = attributesList, ParentNode = node, }); first = false; children.Add(node); } currentIteration.ParentNode.Children = children; currentIteration = stack.Count > 0 ? stack.Pop() : null; } }