示例#1
0
        // Budujemy drzewo
        public TreeNode MountTree(List <List <int> > aSystem, List <Atrybut> aAtrybuty)
        {
            int decision;

            if (AllDecisionOne(aSystem, out decision))
            {
                return(new TreeNode("decision: " + Convert.ToString(decision)));
            }
            if (aAtrybuty.Count == 0)
            {
                return(new TreeNode("decision: " + Convert.ToString(getMostCommonValue(aSystem))));
            }

            this.nNegatives = CountTotalNegatives(aSystem);
            this.nPositives = aSystem.Count() - this.nNegatives;

            this.dEntropy = GetEntropy(this.nPositives, this.nNegatives);

            Atrybut oBestAtri = GetBestAtribute(aSystem, aAtrybuty);

            TreeNode root = new TreeNode(oBestAtri.ToString());

            foreach (int value in oBestAtri.aValues)
            {
                TreeNode           subroot    = root.AddTreeNode(new TreeNode("value: " + Convert.ToString(value)));
                List <List <int> > aSystemC   = new List <List <int> >();
                List <Atrybut>     aAtrybutyC = new List <Atrybut>();

                //zbior obiektow zawierajacy atrybut - bez klonowania !
                foreach (List <int> Obj in aSystem)
                {
                    if (Obj[oBestAtri.nNumerKol] == value)
                    {
                        aSystemC.Add(Obj);
                    }
                }

                //atrybuty bez wybranego
                foreach (Atrybut item in aAtrybuty)
                {
                    if (item != oBestAtri)
                    {
                        aAtrybutyC.Add(item);
                    }
                }

                if (aSystemC.Count == 0)
                {
                    return(new TreeNode("decision: " + Convert.ToString(getMostCommonValue(aSystemC))));
                }
                else
                {
                    DecisionTree id3     = new DecisionTree();
                    TreeNode     subtree = id3.MountTree(aSystemC, aAtrybutyC);
                    subroot.AddTreeNode(subtree);
                }
            }

            return(root);
        }