Ejemplo n.º 1
0
        private static TreeNode GetRootNode(DataTable data, string edge)
        {
            var attributes = new List <MyAttribute>();
            var highestInformationGainIndex = -1;
            var highestInformationGain      = double.MinValue;

            // Get all names, amount of attributes and attributes for every column
            for (var i = 0; i < data.Columns.Count - 1; i++)
            {
                var differentAttributenames = MyAttribute.GetDifferentAttributeNamesOfColumn(data, i);
                attributes.Add(new MyAttribute(data.Columns[i].ToString(), differentAttributenames));
            }

            // Calculate Entropy (S)
            var tableEntropy = CalculateTableEntropy(data);

            for (var i = 0; i < attributes.Count; i++)
            {
                attributes[i].InformationGain = GetGainForAllAttributes(data, i, tableEntropy);

                if (attributes[i].InformationGain > highestInformationGain)
                {
                    highestInformationGain      = attributes[i].InformationGain;
                    highestInformationGainIndex = i;
                }
            }

            try
            {
                return(new TreeNode(attributes[highestInformationGainIndex].Name, highestInformationGainIndex, attributes[highestInformationGainIndex], edge));
            }catch (Exception e)
            {
                return(null);
            }
        }
 public TreeNode(string name, int tableIndex, MyAttribute nodeAttribute, string edge)
 {
     Name          = name;
     TableIndex    = tableIndex;
     NodeAttribute = nodeAttribute;
     ChildNodes    = new List <TreeNode>();
     Edge          = edge;
 }