コード例 #1
0
        static void Main(string[] args)
        {
            //Outlook
            Attribute outlook = new Attribute("Outlook",
                                              new string[] { "sunny", "overcast", "rainy" });
            //Temperature
            Attribute temp = new Attribute("Temperature",
                                           new string[] { "hot", "mild", "cool" });
            //Humidity
            Attribute humidity = new Attribute("Humidity",
                                               new string[] { "high", "normal" });
            //Windy
            Attribute windy = new Attribute("Windy",
                                            new string[] { "true", "false" });

            //načíst data z CSV
            Attribute[] attributes = new Attribute[] { outlook, temp, humidity, windy };
            DataTable   samples    = loadCSV();

            //ID3 na data
            ID3  id3  = new ID3();
            Node root = id3.makeTree(samples, "play", attributes);

            Console.ReadKey();

            printNode(root, "--");
            Console.ReadKey();
        }
コード例 #2
0
        private Node ID3Tree(DataTable DataSet, string Target, Attribute[] attributes)
        {
            //Entropie tabulky je 0.00 = JE LEAF
            if (IsPositive(DataSet, Target) == true)
            {
                Node tmpNode = new Node(new Attribute(true));
                tmpNode.totalData = DataSet.Rows.Count;
                return(tmpNode);
            }
            if (IsNegative(DataSet, Target) == true)
            {
                Node tmpNode = new Node(new Attribute(false));
                tmpNode.totalData = DataSet.Rows.Count;
                return(tmpNode);
            }

            //OR

            TotalLines = 14;
            SearchFor  = Target;
            TrueNumber = Count(DataSet);

            //vypočteme entropii setu
            Console.WriteLine("++++++++++++SET++++++++++++++");
            EntropyOfSet = Entropy(TrueNumber, TotalLines - TrueNumber);
            Console.WriteLine("+++++++++++++++++++++++++++++");

            //Najit "nejlepší atribut" - který má největší information gain v tabulce
            Attribute bestAttribute = GetBestGain(DataSet, attributes);

            Console.WriteLine("BEST GAIN IN SET: " + bestAttribute.AttributeName);
            Console.WriteLine();

            //Udělat z nejlepšího atributu kořen (node)
            Node root = new Node(bestAttribute);

            root.totalData = TotalLines;
            DataTable aSample = DataSet.Clone();


            //Cyklus pro každou možnou hodnotu kterou muže nabývat "Best" atribut
            foreach (string value in bestAttribute.values)
            {
                //Všechny řádky atributu nabývající danou hodnotu
                aSample.Rows.Clear();
                DataRow[] rows = DataSet.Select(bestAttribute.AttributeName + " = " + "'" + value + "'");
                foreach (DataRow row in rows)
                {
                    aSample.Rows.Add(row.ItemArray);
                }

                // nový list atributu bez "best gain" attributu
                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]);
                    }
                }

                //Rekurze ID3 algoritmu na novou pod-tabulku (omezený DataSet)
                //Vkládat node do nodu vytvořeného z best atributu
                ID3  nextTree  = new ID3();
                Node ChildNode = nextTree.makeTree(aSample, Target, (Attribute[])aAttributes.ToArray(typeof(Attribute)));
                root.AddTreeNode(ChildNode, value);
            }

            return(root);
        }