Example #1
0
        public static DecisionRuleSet FromDecisionTree(DecisionTree tree, Dictionary <string, AttributeListInfo> attributes, AttributeType dvt)
        {
            _attributes          = attributes;
            decisionVariableType = dvt;
            //creates a new decision set from decision tree
            var rules = new List <DecisionRule>();

            foreach (var node in tree)
            {
                if (node.IsLeaf && !node.IsRoot && node.Output.HasValue)
                {
                    rules.Add(DecisionRule.NewRuleFromNode(node));
                }
            }

            return(new DecisionRuleSet(rules)
            {
                OutputClassesInSet = tree.Outputs
            });
        }
Example #2
0
        public static DecisionRule NewRuleFromNode(Node node)
        {
            Node         current = node;
            DecisionTree owner   = current.Owner;
            double       output  = current.Output.Value;

            var backgroundRules = new List <DecisionRuleBackground>();

            while (current.Parent != null)
            {
                int    index      = current.Parent.Branches.Index;
                string comparison = current.Comparison;
                double value      = current.Value.Value;

                backgroundRules.Insert(0, new DecisionRuleBackground(index, comparison, value));

                current = current.Parent;
            }

            return(new DecisionRule(node.Owner.Attributes, output, backgroundRules));
        }
Example #3
0
        private void Learn()
        {
            using (ArffReader arffReader = new ArffReader(filePath))
            {
                header        = arffReader.ReadHeader();
                attributes    = new Dictionary <string, AttributeListInfo>(); //attribute name, attribute info
                attributeList = header.Attributes.ToList();

                foreach (var attr in attributeList)
                {
                    string[] array = attr.Type.ToString().Split(',');
                    if (decisionVariableType == AttributeType.Discrete && (array[0].ToString() == "numeric" || array[0].ToString() == "real" || array.Length == 1))
                    {
                        throw new InvalidOperationException("Variable Type Should be Continous, not Discrete");
                    }

                    List <string> myList = new List <string>();
                    foreach (string s in array)
                    {
                        //if s contains real, do decision tree learning for real values
                        myList.Add(s.Replace("{", "").Replace("}", "").Trim());
                    }

                    AttributeListInfo myAttributeInfo = new AttributeListInfo(); //define information for each attribute, then add to dictionary
                    myAttributeInfo.NumberOfInputs = myList.Count;               //array.Length
                    myAttributeInfo.Inputs         = myList;

                    attributes.Add(attr.Name, myAttributeInfo);

                    if (_attributes.Count < attributeList.Count - 1)
                    {
                        _attributes.Add(new Attribute(attr.Name, myAttributeInfo.NumberOfInputs));
                        queryList.Add(new Query(attr.Name, myList));
                    }
                    else
                    {
                        lastAttrName = attr.Name;
                    }
                }

                List <double[]> inputs2   = new List <double[]>();
                List <double[]> inputsAll = new List <double[]>();
                List <int>      outputs2  = new List <int>();
                object[]        instance;
                while ((instance = arffReader.ReadInstance()) != null)
                {
                    int      Length = instance.Length;
                    double[] newRow = new double[Length - 1];
                    double[] row    = new double[Length];
                    for (int i = 0; i < Length - 1; i++)
                    {
                        Double.TryParse(instance[i] + "", out newRow[i]);
                    }
                    for (int i = 0; i < Length; i++)
                    {
                        Double.TryParse(instance[i] + "", out row[i]);
                    }
                    inputs2.Add(newRow);
                    inputsAll.Add(row);
                    outputs2.Add(Int32.Parse(instance[Length - 1] + ""));
                }

                dInputs    = inputs2.ToArray();     //inputs (expect the last index)
                allDInputs = inputsAll.ToArray();   //all double inputs
                outputs    = outputs2.ToArray();

                //decide what algorithm to use based on options.
                if (learningAlgorithm == LearningAlgorithm.C45Learning && decisionVariableType == AttributeType.Continuous)
                {
                    c45 = new C45Algorithm();
                    var stopwatch = new Stopwatch();
                    stopwatch.Start();
                    tree = c45.Learn(dInputs, outputs);   //induce tree from data
                    stopwatch.Stop();
                    elapsedTime = stopwatch.ElapsedMilliseconds;
                }
                else if (learningAlgorithm == LearningAlgorithm.C45Learning && decisionVariableType == AttributeType.Both)
                {
                    c45 = new C45Algorithm();
                    var stopwatch = new Stopwatch();
                    stopwatch.Start();
                    tree = c45.Learn(dInputs, outputs);   //induce tree from data
                    stopwatch.Stop();
                    elapsedTime = stopwatch.ElapsedMilliseconds;
                }
                else if (learningAlgorithm == LearningAlgorithm.C45Learning && decisionVariableType == AttributeType.Discrete)
                {
                    c45 = new C45Algorithm(_attributes.ToArray());
                    var stopwatch = new Stopwatch();
                    stopwatch.Start();
                    tree = c45.Learn(dInputs, outputs);   //induce tree from data
                    stopwatch.Stop();
                    elapsedTime = stopwatch.ElapsedMilliseconds;
                }
                rules = tree.ToRules(attributes, decisionVariableType);

                predicted = tree.ComputeDecision(dInputs);
            }
        }
Example #4
0
 public Node(DecisionTree owner)
 {
     Owner      = owner;
     Comparison = string.Empty;
     Branches   = new SplitProperties(this);
 }