public ConditionalTree BuildConditionalTree()
        {
            var listRows = new List<int>();

            for (int index = 0; index < _data.Count; index++)
            {
                listRows.Add(index);
            }

            if (listRows.Count == 0)
            {
                return null;
            }
            var conditionalTree = new ConditionalTree
            {
                Root = new ConditionalTree.ConditionalNode()
            };

            var attributes = new List<int>();

            for (int index = 0; index < _data.Attributes.Length; index++)
            {
                if (_data.Attributes[index] == TableData.ClassAttributeName)
                {
                    continue;
                }
                attributes.Add(index);
            }
            //BuildConditionalNodesRecursive(listRows, attributes, conditionalTree.Root);
            BuildConditionalNodes(listRows, attributes, conditionalTree.Root);
            return conditionalTree;
        }
Beispiel #2
0
        public ConditionalTree BuildConditionalTree(ITableData data)
        {
            var listRows = data.ToList();
            if (listRows.Count == 0)
            {
                return null;
            }
            var firstRow = listRows[0];
            var conditionaTree = new ConditionalTree
            {
                Root = new ConditionalTree.ConditionalNode()
            };

            var attributes = firstRow.Attributes.ToList();
            attributes.Remove(TableData.ClassAttributeName);
            BuildConditionalNodesRecursive(listRows, attributes, conditionaTree.Root);
            return conditionaTree;
        }
Beispiel #3
0
        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;
            }
        }