Ejemplo n.º 1
0
        // giải thuật ID3

        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 1";
                return(new TreeNode(new Attribute("1")));
            }
            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 0";
                return(new TreeNode(new Attribute("0")));
            }
            if (Attribute.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(Examples))));
            }
            Attribute BestAttribute = GetBestAttribute(Examples, Attribute, bestat);
            int       LocationBA    = Attributes.IndexOf(BestAttribute);
            TreeNode  Root          = new TreeNode(BestAttribute);

            for (int i = 0; i < BestAttribute.Value.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]));
                }
            }
            return(Root);
        }
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;
           }