Beispiel #1
0
        private string toString(DecisionRuleBackground drb, Dictionary <string, AttributeListInfo> attributes)
        {
            int    index = drb.VariableIndex;
            String name  = Variables[index].Name;

            if (String.IsNullOrEmpty(name))
            {
                name = "x[" + index + "]";
            }

            String op = drb.Comparison;

            String value;


            try
            {
                if (attributeType == AttributeType.Discrete)
                {
                    AttributeListInfo attributeInfo = attributes.ElementAt(Int32.Parse(name)).Value;
                    string            originalName  = attributes.ElementAt(Int32.Parse(name)).Key;

                    if (attributeInfo.NumberOfInputs <= 1 || attributeInfo.Inputs[0].ToString() == "real" || attributeInfo.Inputs[0].ToString() == "numeric")
                    {
                        value = drb.Value.ToString();
                    }
                    else
                    {
                        value = attributeInfo.Inputs[(int)drb.Value];   //index of value
                    }


                    return(String.Format("{0} {1} {2}", originalName, op, value));
                }
                else
                {
                    AttributeListInfo attributeInfo = attributes.ElementAt(Int32.Parse(name)).Value;
                    string            originalName  = attributes.ElementAt(Int32.Parse(name)).Key;

                    return(String.Format("{0} {1} {2}", originalName, op, drb.Value.ToString()));
                }
            }
            catch (Exception ex) { }

            value = attributes[name].Inputs[(int)drb.Value];   //index of value
            return(String.Format("{0} {1} {2}", name, op, value));
        }
Beispiel #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);
            }
        }