public void Print(ID3 node, string result) { if (node.Parent == null) { result += "If " + node.Best; } if (node.Children == null || node.Children.Count == 0) { Console.WriteLine(result += ", THEN Play == " + node.Label + "."); } foreach (var child in node.Children) { var oldResult = result; if (child.IsAttribute) { result += " AND " + child.Best; } if (child.IsValue) { result += " == " + child.Value; } Print(child, result); result = oldResult; } }
public ID3(string val, ID3 parent, List <PieceOfData> data) { Value = val; Parent = parent; TrainData = data; Children = new List <ID3>(); }
public void Split(List <PieceOfData> data, string target) { Best = GetBest(data, target); var possibleValues = TrainData.Select(x => x.Attributes[GetIndex(Best)]).Distinct().ToArray(); ID3 parentNode = null; if (Parent != null) { parentNode = new ID3(Best, this); parentNode.Best = Best; parentNode.IsAttribute = true; parentNode.IsValue = false; Children.Add(parentNode); } else { parentNode = this; IsAttribute = true; } foreach (var v in possibleValues) { var child = new ID3(v, parentNode, TrainData); child.IsValue = true; child.IsAttribute = false; parentNode.Children.Add(child); child.Eval(data, target, Best); } return; }
static void Main(string[] args) { #region DataTable DataTable dataTable = new DataTable("PlayGolf"); dataTable.Columns.Add("Outlook"); dataTable.Columns.Add("Temperature"); dataTable.Columns.Add("Humidity"); dataTable.Columns.Add("Wind"); dataTable.Columns.Add("Play"); dataTable.Rows.Add("Sunny", "Hot", "High", "Weak", "No"); dataTable.Rows.Add("Sunny", "Hot", "High", "Strong", "No"); dataTable.Rows.Add("Overcast", "Hot", "High", "Weak", "Yes"); dataTable.Rows.Add("Rain", "Mild", "High", "Weak", "Yes"); dataTable.Rows.Add("Rain", "Cool", "Normal", "Weak", "Yes"); dataTable.Rows.Add("Rain", "Cool", "Normal", "Strong", "No"); dataTable.Rows.Add("Overcast", "Cool", "Normal", "Strong", "Yes"); dataTable.Rows.Add("Sunny", "Mild", "High", "Weak", "No"); dataTable.Rows.Add("Sunny", "Cool", "Normal", "Weak", "Yes"); dataTable.Rows.Add("Rain", "Mild", "Normal", "Weak", "Yes"); dataTable.Rows.Add("Sunny", "Mild", "Normal", "Strong", "Yes"); dataTable.Rows.Add("Overcast", "Mild", "High", "Strong", "Yes"); dataTable.Rows.Add("Overcast", "Hot", "Normal", "Weak", "Yes"); dataTable.Rows.Add("Rain", "Mild", "High", "Strong", "No"); #endregion var data = new List <PieceOfData>(); var allData = new List <PieceOfData>(); int counter = 0; string line; string path = @"..\..\Data\data.txt"; // Read the file and display it line by line. System.IO.StreamReader file = new System.IO.StreamReader(path); while ((line = file.ReadLine()) != null) { var split = line.Split(','); var newData = new PieceOfData(); for (int i = 0; i < 5; i++) { newData.Attributes[i] = split[i]; } newData.Outlook = split[0]; newData.Temperature = split[1]; newData.Humidity = split[2]; newData.Windy = split[3]; newData.Play = split[4]; data.Add(newData); counter++; } var dTree = new ID3(data, "Play"); var result = ""; dTree.Print(dTree, result); Console.ReadLine(); }
public ID3(string name, ID3 parent) { Name = name; Parent = parent; Children = new List <ID3>(); }