public void AddTreeNode(TreeNode treeNode, string valueName) { int index = Attribute.IndexValue(valueName); _childs[index] = treeNode; }
private static void PrintNode(TreeNode root, string tabs) { Console.WriteLine(tabs + '|' + root.Attribute + '|'); if (root.Attribute.Values == null) return; foreach (string t in root.Attribute.Values) { Console.WriteLine(tabs + "\t" + "<" + t + ">"); TreeNode childNode = root.GetChildByBranchName(t); PrintNode(childNode, "\t" + tabs); } }
private TreeNode InternalMountTree(DataTable samples, string targetAttribute, IReadOnlyCollection<Attribute> attributes) { if (AllSamplesPositives(samples, targetAttribute)) return new TreeNode(new Attribute(true)); if (AllSamplesNegatives(samples, targetAttribute)) return new TreeNode(new Attribute(false)); if (attributes.Count == 0) return new TreeNode(new Attribute(GetMostCommonValue(samples, targetAttribute))); _total = samples.Rows.Count; _targetAttribute = targetAttribute; _totalPositives = CountTotalPositives(samples); _mEntropySet = CalcEntropy(_totalPositives, _total - _totalPositives); Attribute bestAttribute = GetBestAttribute(samples, attributes); TreeNode root = new TreeNode(bestAttribute); DataTable aSample = samples.Clone(); foreach (string value in bestAttribute.Values) { // Select all the elements with this Attribute value aSample.Rows.Clear(); DataRow[] rows = samples.Select(bestAttribute.AttributeName + " = " + "'" + value + "'"); foreach (DataRow row in rows) aSample.Rows.Add(row.ItemArray); // Select all the elements with this Attribute value // Create a new list of attributes less the current Attribute that is the best Attribute ArrayList aAttributes = new ArrayList(attributes.Count - 1); foreach ( Attribute attribute in attributes.Where(attribute => attribute.AttributeName != bestAttribute.AttributeName)) aAttributes.Add(attribute); // Create a new list of attributes less the current Attribute that is the best Attribute if (aSample.Rows.Count == 0) return new TreeNode(new Attribute(GetMostCommonValue(aSample, targetAttribute))); DecisionTreeId3 dc3 = new DecisionTreeId3(); TreeNode childNode = dc3.MountTree(aSample, targetAttribute, (Attribute[])aAttributes.ToArray(typeof(Attribute))); root.AddTreeNode(childNode, value); } return root; }