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;
                }
            }

            return(new TreeNode(attributes[highestInformationGainIndex].Name, highestInformationGainIndex, attributes[highestInformationGainIndex], edge));
        }
Ejemplo n.º 2
0
        public static DataTable ImportFromCsvFile(string filePath)
        {
            var rows = 0;
            var data = new DataTable();

            try
            {
                using (var reader = new StreamReader(File.OpenRead(filePath)))
                {
                    while (!reader.EndOfStream)
                    {
                        var line   = reader.ReadLine();
                        var values = line.Substring(0, line.Length - 1).Split(';');

                        foreach (var item in values)
                        {
                            if (string.IsNullOrEmpty(item) || string.IsNullOrWhiteSpace(item))
                            {
                                throw new Exception("Value can't be empty");
                            }

                            if (rows == 0)
                            {
                                data.Columns.Add(item);
                            }
                        }

                        if (rows > 0)
                        {
                            data.Rows.Add(values);
                        }

                        rows++;

                        if (values.Length != data.Columns.Count)
                        {
                            throw new Exception("Row is shorter or longer than title row");
                        }
                    }
                }

                var differentValuesOfLastColumn =
                    MyAttribute.GetDifferentAttributeNamesOfColumn(data, data.Columns.Count - 1);

                Console.WriteLine("data.Columns.Count -> " + data.Columns.Count);
                Console.WriteLine("differentValuesOfLastColumn.Count -> " + differentValuesOfLastColumn.Count);
                if (differentValuesOfLastColumn.Count > 4)
                {
                    throw new Exception("The last column is the result column and can contain only 4 different values");
                }
            }
            catch (Exception ex)
            {
                DisplayErrorMessage(ex.Message);
                data = null;
            }

            // if no rows are entered or data == null, return null
            return(data?.Rows.Count > 0 ? data : null);
        }
Ejemplo n.º 3
0
 public TreeNode(string name, int tableIndex, MyAttribute nodeAttribute, string edge)
 {
     Name          = name;
     TableIndex    = tableIndex;
     NodeAttribute = nodeAttribute;
     ChildNodes    = new List <TreeNode>();
     Edge          = edge;
 }