Ejemplo n.º 1
0
 // lấy độ sâu của cây
     private int GetDepth(TreeNode tree) 
     {
         int depth;
         if (tree.Childs.Length == 0)
             return 1;
         else
         {
             depth = GetDepth(tree.Childs[0]);
             for (int i = 1; i < tree.Childs.Length; i++) 
             {
                 int depthchild = GetDepth(tree.Childs[i]);
                 if (depth < depthchild) depth = depthchild;
             }
             depth++; 
         }
         return depth; 
     }
Ejemplo n.º 2
0
           private TreeNode ID3(List<List<string>> Examples, List<Attribute> Attribute, string bestat)
           {
               Solution = Solution + " Xét " + bestat + " ";

               if (CheckAllPositive(Examples))
               {
                   Solution += "\n Tất cả các mẫu đều khẳng định => Trả về nút gốc với nhãn Yes";
                   return new TreeNode(new Attribute("Yes"));
               }
               if (CheckAllNegative(Examples))
               {

               }
               Solution += "\n Tất cả các mẫu đều phủ định => Trả về nút gốc với nhãn No";
               return new TreeNode(new Attribute("No"));
               if (Attribute.Count == 0)
               {
                   return new TreeNode(new Attribute(GetMostCommonValue(Examples)));
               }
               Attribute BestAttribute = GetBestAttribute(Examples, Attribute, bestat);
               int LocationBA = Attributes.IndexOf(BestAttribute);
               TreeNode Root = new TreeNode(BestAttribute);
               for (int i = 0; i < BestAttribute.LValue.Count; i++)
               {
                   List<List<string>> Examplesvi = new List<List<string>>();
                   for (int j = 0; j < Examples.Count; j++)
                   {
                       if (Examples[j][LocationBA].ToString() == BestAttribute.Value[i].ToString()) 
                           Examplesvi.Add(Examples[j]);
                   }
                   if (Examplesvi.Count == 0)
                   {
                       Solution += "\n Các thuộc tính rỗng => Trả về nút gốc có giá trị  phổ biến nhất ";
                       return new TreeNode(new Attribute(GetMostCommonValue(Examplesvi)));
                   }
                   else
                   {
                       Solution += "\n";
                       Attribute.Remove(BestAttribute);
                       //Root.AddNode(ID3(Examplesvi, Attribute, BestAttribute.Value[i]));
                       Root.AddNode(ID3(Examplesvi, Attribute, BestAttribute.Value[i].ToString()));
                   }
               }
               return Root;
           }
Ejemplo n.º 3
0
 public void DeleteTree(TreeNode tree) // hàm làm cây rỗng. 
 {
     tree.Attributes.Name=""; //tree.Attributes.Label=null; 
     tree.Attributes.LValue.Clear();
 }
Ejemplo n.º 4
0
 public void OptimizeTree(TreeNode tree) 
 {
     for (int i = 0; i < tree.Attributes.LValue.Count; i++) 
     {
         if (tree.Attributes.LValue.Count > 1) 
         {
             if (CheckAllLabelPositive(tree)) 
             {
                 tree.Attributes.Label = "Yes";
                 DeleteTree(tree); 
             }
             else
                  OptimizeTree(tree.Childs[i]);
             if (CheckAllLabelNegative(tree)) 
             {
                 tree.Attributes.Label = "No";
                 DeleteTree(tree); 
             }
             else
                 OptimizeTree(tree.Childs[i]);
         } 
     }
 }
Ejemplo n.º 5
0
 public void GetRule(TreeNode tree)
 {
     Solution1 = "";
     Rule += " RÚT RA LUẬT TỪ CÂY QUYẾT ĐỊNH ID3 \n\n"; SearchRule(tree);
     for (int i = 0; i < RuleCount; i++)
         Rule += " Rule [" + i + "]: IF {" + RuleID3[i] + "\n"; Rule += "\n Tổng Số Luật: " + RuleCount;
     RuleCount = 0;
 }
Ejemplo n.º 6
0
 public bool CheckAllLabelPositive(TreeNode tree) 
 {
     int test=0;
     string temp;
     temp = "Yes";
     for (int i = 0; i < tree.Attributes.LValue.Count; i++) 
     {
         if (tree.Childs[i].Attributes.Label == temp)
             test++; 
     }
     if ((test>1)&&(test == tree.Attributes.LValue.Count)) 
         return true;
     else
         return false;
 }
Ejemplo n.º 7
0
 public void AddTreeNode(TreeNode treeNode, string ValueName)
 {
     int index = mAttribute.indexValue(ValueName);
     mChilds[index] = treeNode;
 }
Ejemplo n.º 8
0
 public void SearchRule(TreeNode Rule) 
 {
     if (Rule.Attributes.LValue.Count != 0) 
     {
         string temp1="";
         Solution1 += Rule.Attributes.Name + " = ";
         temp1 += Solution1+ " ";
         for (int i = 0; i < Rule.Attributes.LValue.Count; i++)
         {
             string temp2 = "";
             temp2 = temp1 + Rule.Attributes.Value[i] + ", "; 
             if (Rule.Childs[i].Attributes.LValue.Count == 0) 
             {
                 RuleCount++;
                 Solution1 = temp2 + "} THEN {"+Rule.Childs[i].Attributes.Label+"}";
                 RuleID3.Add(Solution1); 
             }
             else
             {
                 if (Rule.Attributes.LValue.Count == 0) 
                 {
                     SearchRule(Rule.Childs[i]); }
                 else
                 {
                     Solution1 = temp2; SearchRule(Rule.Childs[i]);
                 }
             }
         }
     }
 }
Ejemplo n.º 9
0
        void DumpVisualTree(TreeViewItem parentNode, TreeNode root)
        {
            TreeViewItem item = new TreeViewItem();
            item.Header = ": " + root.attribute.ToString();
            parentNode.Items.Add(item);

            int count = root.totalChilds;
            if (root.attribute.values != null)
            {
                for (int i = 0; i < count; i++)
                {
                    item = new TreeViewItem();
                    item.Header = ": " + root.attribute.values[i].ToString();
                    parentNode.Items.Add(item);
                    TreeNode child = root.getChild(i);
                    DumpVisualTree(item, child);
                }
            }
        }
Ejemplo n.º 10
0
 public TreeNode AddNode(TreeNode tree) //Thêm hàm
 {
     return this;
 }
Ejemplo n.º 11
0
 public void SearchRule(TreeNode Rule)
 {
     if (Rule.attribute.values != null)
     {
         string temp1 = "";
         Solution1 += Rule.attribute.AttributeName + " = ";
         temp1 += Solution1 + " ";
         for (int i = 0; i < Rule.attribute.values.Length; i++)
         {
             string temp2 = "";
             temp2 = temp1 + Rule.attribute.values[i] + ", ";
             TreeNode childNode = Rule.getChildByBranchName(Rule.attribute.values[i]);
             if (childNode.attribute.values == null)
             {
                 RuleCount++;
                 Solution1 = temp2 + "} THEN {" + childNode.attribute.mLabel + "}";
                 RuleID3.Add(Solution1);
             }
             else
             {
                 if (Rule.attribute.values == null)
                 {
                     SearchRule(childNode);
                 }
                 else
                 {
                     Solution1 = temp2;
                     SearchRule(childNode);
                 }
             }
         }
     }
 }
Ejemplo n.º 12
0
 public static void printNode(TreeNode root, string tabs)
 {
    
     Console.WriteLine(tabs + '|' + root.attribute + '|');
     TreeList += "\n" + tabs + '|' + root.attribute + '|';
     if (root.attribute.values != null)
     {
         for (int i = 0; i < root.attribute.values.Length; i++)
         {
             Console.WriteLine(tabs + "\t" + "<" + root.attribute.values[i] + ">");
             TreeList += "\n" + tabs + "\t" + "<" + root.attribute.values[i] + ">";
             TreeNode childNode = root.getChildByBranchName(root.attribute.values[i]);
             printNode(childNode, "\t" + tabs);
         }
     }
 }
Ejemplo n.º 13
0
        private TreeNode internalMountTree(DataTable samples, string targetAttribute, Attribute[] attributes)
        {
            if (allSamplesPositives(samples, targetAttribute) == true)
                return new TreeNode(new Attribute(true));

            if (allSamplesNegatives(samples, targetAttribute) == true)
                return new TreeNode(new Attribute(false));

            if (attributes.Length == 0)
                return new TreeNode(new Attribute(getMostCommonValue(samples, targetAttribute)));

            mTotal = samples.Rows.Count;
            mTargetAttribute = targetAttribute;
            mTotalPositives = countTotalPositives(samples);

            mEntropySet = calcEntropy(mTotalPositives, mTotal - mTotalPositives);

            Attribute bestAttribute = getBestAttribute(samples, attributes);

            TreeNode root = new TreeNode(bestAttribute);

            DataTable aSample = samples.Clone();

            foreach (string value in bestAttribute.values)
            {
                			
                aSample.Rows.Clear();

                DataRow[] rows = samples.Select(bestAttribute.AttributeName + " = " + "'" + value + "'");
                foreach (DataRow row in rows)
                {
                    aSample.Rows.Add(row.ItemArray);
                }
                		
                ArrayList aAttributes = new ArrayList(attributes.Length - 1);
                for (int i = 0; i < attributes.Length; i++)
                {
                    if (attributes[i].AttributeName != bestAttribute.AttributeName)
                        aAttributes.Add(attributes[i]);
                }

                if (aSample.Rows.Count == 0)
                {
                    return new TreeNode(new Attribute(getMostCommonValue(aSample, targetAttribute)));
                }
                else
                {
                    DecisionTree dc3 = new DecisionTree();
                    TreeNode ChildNode = dc3.mountTree(aSample, targetAttribute, (Attribute[])aAttributes.ToArray(typeof(Attribute)));
                    root.AddTreeNode(ChildNode, value);
                }
            }       
            return root;
        }