コード例 #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);
        }
コード例 #2
0
        private TreeNode UtworzDrzewo()
        {
            TreeNode root;

            List <Atrybut> aAtrybuty = new List <Atrybut>();

            for (int i = 0; i < (this.aSystem[0].Count - 1); i++)
            {
                List <int> aWartosci = new List <int>(0);

                for (int j = 0; j < this.aSystem.Count; j++)
                {
                    aWartosci.Add(this.aSystem[j][i]);
                }

                aAtrybuty.Add(new Atrybut(i, aWartosci));
            }

            DecisionTree ID3 = new DecisionTree();

            root = ID3.MountTree(this.aSystem, aAtrybuty);

            return(root);
        }