Esempio n. 1
0
 //   Transforms the tree into a set of decision rules</see>.
 public DecisionRuleSet ToRules(Dictionary <string, AttributeListInfo> attributes, AttributeType dvt)
 {
     return(DecisionRuleSet.FromDecisionTree(this, attributes, dvt));
 }
Esempio n. 2
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);
            }
        }