示例#1
0
 public static String findValueInTree(DTNode root, Example test)
 {
     if (root.getChildren().Count > 0)
     {
         double value;
         // foreach child
         foreach (KeyValuePair<DTNode, Condition> entry in root.getChildren())
         {
             switch (entry.Value.getOperator())
             {
                 case Constants.conditionType.GEQT:
                     if (test.getAttributes().TryGetValue(entry.Value.getMember1(), out value) == true)
                     {
                         if (value >= entry.Value.getMember2())
                             return findValueInTree(entry.Key, test);
                     }
                     break;
                 case Constants.conditionType.LT:
                     if (test.getAttributes().TryGetValue(entry.Value.getMember1(), out value) == true)
                     {
                         if (value < entry.Value.getMember2())
                             return findValueInTree(entry.Key, test);
                     }
                     break;
                 default:
                     break;
             }
         }
     }
     return root.getResult();
 }
示例#2
0
        public static void printTree(DTNode root)
        {
            if (root.getChildren().Count == 0)
            {
                Console.WriteLine(" => " + root.getResult() + "\n");
            }
            else
            {
                foreach (KeyValuePair <DTNode, Condition> entry in root.getChildren())
                {
                    // do something with entry.Value or entry.Key
                    Console.Write(entry.Value.getMember1() + " ");
                    switch (entry.Value.getOperator())
                    {
                    case Constants.conditionType.LT:
                        Console.Write(" < "); break;

                    case Constants.conditionType.GEQT:
                        Console.Write(" >= "); break;

                    default: break;
                    }
                    Console.Write(entry.Value.getMember2() + "\n");
                    printTree(entry.Key);
                }
            }
        }
示例#3
0
 public static bool isTrivial(DTNode node)
 {
     for (int i = 0; i < node.getTrainingSet().Count - 1; i++)
     {
         if (node.getTrainingSet()[i].getResult().Equals
                 (node.getTrainingSet()[i + 1].getResult()) == false)
         {
             return(false);
         }
     }
     return(true);
 }
示例#4
0
 public static void printTree(DTNode root)
 {
     if (root.getChildren().Count == 0)
         Console.WriteLine(" => " + root.getResult() + "\n");
     else
     {
         foreach (KeyValuePair<DTNode, Condition> entry in root.getChildren())
         {
             // do something with entry.Value or entry.Key
             Console.Write(entry.Value.getMember1() + " ");
             switch (entry.Value.getOperator())
             {
                 case Constants.conditionType.LT:
                     Console.Write(" < "); break;
                 case Constants.conditionType.GEQT:
                     Console.Write(" >= "); break;
                 default: break;
             }
             Console.Write(entry.Value.getMember2() + "\n");
             printTree(entry.Key);
         }
     }
 }
示例#5
0
        public static String findValueInTree(DTNode root, Example test)
        {
            if (root.getChildren().Count > 0)
            {
                double value;
                // foreach child
                foreach (KeyValuePair <DTNode, Condition> entry in root.getChildren())
                {
                    switch (entry.Value.getOperator())
                    {
                    case Constants.conditionType.GEQT:
                        if (test.getAttributes().TryGetValue(entry.Value.getMember1(), out value) == true)
                        {
                            if (value >= entry.Value.getMember2())
                            {
                                return(findValueInTree(entry.Key, test));
                            }
                        }
                        break;

                    case Constants.conditionType.LT:
                        if (test.getAttributes().TryGetValue(entry.Value.getMember1(), out value) == true)
                        {
                            if (value < entry.Value.getMember2())
                            {
                                return(findValueInTree(entry.Key, test));
                            }
                        }
                        break;

                    default:
                        break;
                    }
                }
            }
            return(root.getResult());
        }
        void Onb2Click(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("Begin training...");

            bodyTS = IOUtils.readBodyTS();
            text2.Text = "Body training set read.";
            leftHandTS = IOUtils.readLeftHandTS();
            text2.Text = "Left hand training set read.";
            rightHandTS = IOUtils.readRightHandTS();
            text2.Text = "Right Hand training set read.";
            leftLegTS = IOUtils.readLeftLegTS();
            text2.Text = "Left leg training set read.";
            rightLegTS = IOUtils.readRightLegTS();
            text2.Text = "Right leg training set read.";

            Console.WriteLine("Initialize attributes and classses...");
            bodyClasses = new List<String>();
            bodyClasses.Add("drept");
            bodyClasses.Add("aplecat frontal");
            bodyClasses.Add("aplecat lateral");
            bodyClasses.Add("asezat");
            bodyClasses.Add("culcat");

            handClasses = new List<String>();
            handClasses.Add("langa corp");
            handClasses.Add("ridicat lateral");
            handClasses.Add("ridicat frontal");
            handClasses.Add("ridicat sus");

            legClasses = new List<String>();
            legClasses.Add("drept");
            legClasses.Add("ridicat lateral");
            legClasses.Add("ridicat frontal");
            legClasses.Add("indoit");

            // set attributes for each decision tree
            attributes = new List<String>();
            attributes.Add("A.2.1.13");
            attributes.Add("A.2.1.17");
            attributes.Add("A.1.2.XoY");
            attributes.Add("A.1.2.YoZ");

            attributes.Add("A.2.1.13");
            attributes.Add("A.2.1.17");
            attributes.Add("A.1.2.XoY");
            attributes.Add("A.1.2.YoZ");
            bodyDT = new DTNode(bodyTS, attributes, bodyClasses);
            
            attributes = new List<String>();
            attributes.Add("A.4.5.XoY");
            attributes.Add("A.4.5.YoZ");
            attributes.Add("D.4y.5y");

            //attributes.Add("A.4.5.XoY");
            //attributes.Add("A.4.5.YoZ");
            //attributes.Add("D.4y.5y");
            leftHandDT = new DTNode(leftHandTS, attributes, handClasses);

            attributes = new List<String>();
            attributes.Add("A.8.9.XoY");
            attributes.Add("A.8.9.YoZ");
            attributes.Add("D.8y.9y");

            attributes.Add("A.8.9.XoY");
            attributes.Add("A.8.9.YoZ");
            attributes.Add("D.8y.9y");
            rightHandDT = new DTNode(rightHandTS, attributes, handClasses);

            attributes = new List<String>();
            attributes.Add("A.12.13.14");
            attributes.Add("A.13.14.XoY");
            attributes.Add("A.13.14.YoZ");

            attributes.Add("A.12.13.14");
            attributes.Add("A.13.14.XoY");
            attributes.Add("A.13.14.YoZ");
            leftLegDT = new DTNode(leftLegTS, attributes, legClasses);

            attributes = new List<String>();
            attributes.Add("A.16.17.18");
            attributes.Add("A.17.18.XoY");
            attributes.Add("A.17.18.YoZ");

            attributes.Add("A.16.17.18");
            attributes.Add("A.17.18.XoY");
            attributes.Add("A.17.18.YoZ");
            rightLegDT = new DTNode(rightLegTS, attributes, legClasses);
            
            Console.WriteLine("Build trees...");
            DTUtils.buildTree(bodyDT);
            Console.WriteLine("Body tree built.");
            DTUtils.buildTree(leftHandDT);
            Console.WriteLine("Left hand tree built.");
            DTUtils.buildTree(rightHandDT);
            Console.WriteLine("Right hand built.");
            DTUtils.buildTree(leftLegDT);
            Console.WriteLine("Left leg built.");
            DTUtils.buildTree(rightLegDT);
            Console.WriteLine("Right leg built.");

            text2.Text = "Done!";
            b1.IsEnabled = false;
            /*
            bodyMM.IsEnabled = true;
            leftHandMM.IsEnabled = true;
            rightHandMM.IsEnabled = true;
            leftLegMM.IsEnabled = true;
            rightLegMM.IsEnabled = true;
             * */
        }
示例#7
0
        public static void buildTree(DTNode root)
        {
            if (isTrivial(root) == true)
            {
                root.setResult(root.getTrainingSet()[0].getResult());
            }
            else if (root.getAttrNames().Count == 0)
            {
                // set as a result the most frequent class in training set
                Dictionary <String, int> classCount = new Dictionary <String, int>();
                foreach (String className in root.getClasses())
                {
                    classCount.Add(className, 0);
                }
                // get entropy of all training set
                foreach (Example example in root.getTrainingSet())
                {
                    int value;
                    if (classCount.TryGetValue(example.getResult(), out value))
                    {
                        classCount[example.getResult()] = value + 1;
                    }
                }
                int    maxCount    = 0;
                String classResult = "";
                foreach (KeyValuePair <String, int> entry in classCount)
                {
                    if (maxCount < entry.Value)
                    {
                        maxCount    = entry.Value;
                        classResult = entry.Key;
                    }
                }

                root.setResult(classResult);
            }
            else
            {
                // find attribute with maximum gain
                GainResult gainResult    = new GainResult();
                GainResult gainResultMax = new GainResult();
                gainResultMax.setGain(0.0);
                int resultIndex = 0;
                for (int i = 0; i < root.getAttrNames().Count; i++)
                {
                    gainResult = getGain(root.getTrainingSet(), root.getAttrNames()[i], root.getClasses());
                    gainResult.setGain(getEntropy(root.getTrainingSet(), root.getClasses(), root.getAttrNames()[i], -1) -
                                       gainResult.getGain());
                    if (gainResultMax.getGain() < gainResult.getGain())
                    {
                        gainResultMax.setGain(gainResult.getGain());
                        gainResultMax.setPartitionIndex(gainResult.getPartitionIndex());
                        resultIndex = i;
                    }
                }
                String attributeNode = root.getAttrNames()[resultIndex];
                root.getAttrNames().RemoveAt(resultIndex);
                // sort training list by the attribute with maximum gain
                root.getTrainingSet().Sort(new ExampleComparator(attributeNode));
                // form children
                for (int i = 0; i < 2; i++)
                {
                    List <String> newAttrNames = new List <String>();
                    foreach (String str in root.getAttrNames())
                    {
                        newAttrNames.Add(str);
                    }

                    List <String> newClassNames = new List <String>();
                    foreach (String str in root.getClasses())
                    {
                        newClassNames.Add(str);
                    }
                    DTNode         child          = new DTNode(new List <Example>(), newAttrNames, newClassNames);
                    List <Example> newTrainingSet = new List <Example>();
                    Condition      condition      = new Condition();
                    if (i == 0)
                    {
                        for (int p = 0; p <= gainResultMax.getPartitionIndex(); p++)
                        {
                            Example example    = root.getTrainingSet()[p];
                            Example newExample = new Example();
                            // copy result
                            newExample.setResult(example.getResult());
                            // copy attribute values
                            Dictionary <String, Double> newAttributes = new Dictionary <String, Double>();
                            foreach (KeyValuePair <String, Double> entry in example.getAttributes())
                            {
                                newAttributes.Add(entry.Key, entry.Value);
                            }
                            newExample.setAttributes(newAttributes);
                            newTrainingSet.Add(newExample);
                        }
                        condition.setOperator(Constants.conditionType.LT);
                    }
                    else
                    {
                        for (int p = gainResultMax.getPartitionIndex() + 1; p < root.getTrainingSet().Count; p++)
                        {
                            Example example    = root.getTrainingSet()[p];
                            Example newExample = new Example();
                            // copy result
                            newExample.setResult(example.getResult());
                            // copy attribute values
                            Dictionary <String, Double> newAttributes = new Dictionary <String, Double>();
                            foreach (KeyValuePair <String, Double> entry in example.getAttributes())
                            {
                                newAttributes.Add(entry.Key, entry.Value);
                            }
                            newExample.setAttributes(newAttributes);
                            newTrainingSet.Add(newExample);
                        }
                        condition.setOperator(Constants.conditionType.GEQT);
                    }
                    condition.setMember1(attributeNode);
                    double leftValue  = root.getTrainingSet()[gainResultMax.getPartitionIndex()].getValue(attributeNode);
                    double rightValue = root.getTrainingSet()[gainResultMax.getPartitionIndex() + 1].getValue(attributeNode);
                    condition.setMember2((leftValue + rightValue) / 2);
                    child.setTrainingSet(newTrainingSet);
                    root.getChildren().Add(child, condition);
                    buildTree(child);
                }
            }
        }
示例#8
0
     public static void buildTree(DTNode root) {
     if (isTrivial(root) == true) {
         root.setResult(root.getTrainingSet()[0].getResult());
     }
     else if (root.getAttrNames().Count == 0) {
         // set as a result the most frequent class in training set
         Dictionary<String, int> classCount = new Dictionary<String, int>(); 
         foreach (String className in root.getClasses()) {
             classCount.Add(className, 0);
         }
         // get entropy of all training set
         foreach (Example example in root.getTrainingSet()) {
             int value;
             if (classCount.TryGetValue(example.getResult(), out value)) 
             {
                 classCount[example.getResult()] = value+1;
             }
             
         }     
         int maxCount = 0;
         String classResult = "";
         foreach(KeyValuePair<String, int> entry in classCount)
         {
             if (maxCount < entry.Value) {
                 maxCount = entry.Value;
                 classResult = entry.Key;
             }
         }
         
         root.setResult(classResult);
     }
     else {
         // find attribute with maximum gain
         GainResult gainResult = new GainResult();
         GainResult gainResultMax = new GainResult();
         gainResultMax.setGain(0.0);
         int resultIndex = 0;
         for (int i=0; i<root.getAttrNames().Count; i++) {
             gainResult = getGain(root.getTrainingSet(), root.getAttrNames()[i], root.getClasses());
             gainResult.setGain(getEntropy(root.getTrainingSet(), root.getClasses(), root.getAttrNames()[i], -1) - 
                     gainResult.getGain());
             if (gainResultMax.getGain() < gainResult.getGain()) {
                 gainResultMax.setGain(gainResult.getGain());
                 gainResultMax.setPartitionIndex(gainResult.getPartitionIndex());
                 resultIndex = i;
             }
         }
         String attributeNode = root.getAttrNames()[resultIndex];
         root.getAttrNames().RemoveAt(resultIndex);
         // sort training list by the attribute with maximum gain
         root.getTrainingSet().Sort(new ExampleComparator(attributeNode));
         // form children
         for (int i=0; i<2; i++)
         {
             List<String> newAttrNames = new List<String>();
             foreach (String str in root.getAttrNames()) {
                 newAttrNames.Add(str);
             }
             
             List<String> newClassNames = new List<String>();
             foreach (String str in root.getClasses()) {
                 newClassNames.Add(str);
             }
             DTNode child = new DTNode(new List<Example>(), newAttrNames, newClassNames);
             List<Example> newTrainingSet = new List<Example>();
             Condition condition = new Condition();
             if (i == 0) {
                 for (int p=0; p <= gainResultMax.getPartitionIndex(); p++) {
                     Example example = root.getTrainingSet()[p];
                     Example newExample = new Example();
                     // copy result
                     newExample.setResult(example.getResult());
                     // copy attribute values
                     Dictionary<String, Double> newAttributes = new Dictionary<String, Double>();
                     foreach (KeyValuePair<String, Double> entry in example.getAttributes())
                     {
                         newAttributes.Add(entry.Key, entry.Value);
                     }
                     newExample.setAttributes(newAttributes);                        
                     newTrainingSet.Add(newExample); 
                 }
                 condition.setOperator(Constants.conditionType.LT);                    
             }
             else {
                 for (int p=gainResultMax.getPartitionIndex()+1; p<root.getTrainingSet().Count; p++) {
                     Example example = root.getTrainingSet()[p];
                     Example newExample = new Example();
                     // copy result
                     newExample.setResult(example.getResult());
                     // copy attribute values
                     Dictionary<String, Double> newAttributes = new Dictionary<String, Double>();
                     foreach (KeyValuePair<String, Double> entry in example.getAttributes())
                     {
                         newAttributes.Add(entry.Key, entry.Value);
                     }
                     newExample.setAttributes(newAttributes);
                     newTrainingSet.Add(newExample);
                 }
                 condition.setOperator(Constants.conditionType.GEQT);
             }
             condition.setMember1(attributeNode);
             double leftValue = root.getTrainingSet()[gainResultMax.getPartitionIndex()].getValue(attributeNode);
             double rightValue = root.getTrainingSet()[gainResultMax.getPartitionIndex()+1].getValue(attributeNode);
             condition.setMember2((leftValue+rightValue)/2);                
             child.setTrainingSet(newTrainingSet);
             root.getChildren().Add(child, condition);                
             buildTree(child);
         }
     }
 }
示例#9
0
 public static bool isTrivial(DTNode node)
 {
     for (int i = 0; i < node.getTrainingSet().Count - 1; i++)
     {                
         if (node.getTrainingSet()[i].getResult().Equals
                 (node.getTrainingSet()[i+1].getResult()) == false)
             return false;
     }
     return true;
 }
示例#10
0
        void Onb2Click(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("Begin training...");

            bodyTS      = IOUtils.readBodyTS();
            text2.Text  = "Body training set read.";
            leftHandTS  = IOUtils.readLeftHandTS();
            text2.Text  = "Left hand training set read.";
            rightHandTS = IOUtils.readRightHandTS();
            text2.Text  = "Right Hand training set read.";
            leftLegTS   = IOUtils.readLeftLegTS();
            text2.Text  = "Left leg training set read.";
            rightLegTS  = IOUtils.readRightLegTS();
            text2.Text  = "Right leg training set read.";

            Console.WriteLine("Initialize attributes and classses...");
            bodyClasses = new List <String>();
            bodyClasses.Add("drept");
            bodyClasses.Add("aplecat frontal");
            bodyClasses.Add("aplecat lateral");
            bodyClasses.Add("asezat");
            bodyClasses.Add("culcat");

            handClasses = new List <String>();
            handClasses.Add("langa corp");
            handClasses.Add("ridicat lateral");
            handClasses.Add("ridicat frontal");
            handClasses.Add("ridicat sus");

            legClasses = new List <String>();
            legClasses.Add("drept");
            legClasses.Add("ridicat lateral");
            legClasses.Add("ridicat frontal");
            legClasses.Add("indoit");

            // set attributes for each decision tree
            attributes = new List <String>();
            attributes.Add("A.2.1.13");
            attributes.Add("A.2.1.17");
            attributes.Add("A.1.2.XoY");
            attributes.Add("A.1.2.YoZ");

            attributes.Add("A.2.1.13");
            attributes.Add("A.2.1.17");
            attributes.Add("A.1.2.XoY");
            attributes.Add("A.1.2.YoZ");
            bodyDT = new DTNode(bodyTS, attributes, bodyClasses);

            attributes = new List <String>();
            attributes.Add("A.4.5.XoY");
            attributes.Add("A.4.5.YoZ");
            attributes.Add("D.4y.5y");

            //attributes.Add("A.4.5.XoY");
            //attributes.Add("A.4.5.YoZ");
            //attributes.Add("D.4y.5y");
            leftHandDT = new DTNode(leftHandTS, attributes, handClasses);

            attributes = new List <String>();
            attributes.Add("A.8.9.XoY");
            attributes.Add("A.8.9.YoZ");
            attributes.Add("D.8y.9y");

            attributes.Add("A.8.9.XoY");
            attributes.Add("A.8.9.YoZ");
            attributes.Add("D.8y.9y");
            rightHandDT = new DTNode(rightHandTS, attributes, handClasses);

            attributes = new List <String>();
            attributes.Add("A.12.13.14");
            attributes.Add("A.13.14.XoY");
            attributes.Add("A.13.14.YoZ");

            attributes.Add("A.12.13.14");
            attributes.Add("A.13.14.XoY");
            attributes.Add("A.13.14.YoZ");
            leftLegDT = new DTNode(leftLegTS, attributes, legClasses);

            attributes = new List <String>();
            attributes.Add("A.16.17.18");
            attributes.Add("A.17.18.XoY");
            attributes.Add("A.17.18.YoZ");

            attributes.Add("A.16.17.18");
            attributes.Add("A.17.18.XoY");
            attributes.Add("A.17.18.YoZ");
            rightLegDT = new DTNode(rightLegTS, attributes, legClasses);

            Console.WriteLine("Build trees...");
            DTUtils.buildTree(bodyDT);
            Console.WriteLine("Body tree built.");
            DTUtils.buildTree(leftHandDT);
            Console.WriteLine("Left hand tree built.");
            DTUtils.buildTree(rightHandDT);
            Console.WriteLine("Right hand built.");
            DTUtils.buildTree(leftLegDT);
            Console.WriteLine("Left leg built.");
            DTUtils.buildTree(rightLegDT);
            Console.WriteLine("Right leg built.");

            text2.Text   = "Done!";
            b1.IsEnabled = false;

            /*
             * bodyMM.IsEnabled = true;
             * leftHandMM.IsEnabled = true;
             * rightHandMM.IsEnabled = true;
             * leftLegMM.IsEnabled = true;
             * rightLegMM.IsEnabled = true;
             * */
        }