public static void Traverse(TreeNode node, DataRow dt, ref string val) { string currentval = string.Empty; if (node.attribute.values != null) { currentval = dt[node.attribute.ToString()].ToString(); bool result = CheckForMissingValue(node.attribute.values, currentval); if (result == true) { if (node.attribute.postives >= node.attribute.negatives) { val = "1"; return; } else if (node.attribute.postives < node.attribute.negatives) { val = "0"; return; } } else { for (int i = 0; i < node.attribute.values.Length; i++) { currentval = dt[node.attribute.ToString()].ToString(); if (currentval == node.attribute.values[i]) { TreeNode childNode = node.getBranchChild(node.attribute.values[i]); Traverse(childNode, dt, ref val); } } } } else { val = node.attribute.aLabel.ToString(); return; } }
/*This function is used to print the decision tree*/ public static void printNode(TreeNode root, string tabs) { if (root.totalCountOfChilds == 1) { Console.Write(root.attribute); Console.Write(Environment.NewLine); } else { Console.Write(tabs + "|" + root.attribute); } if (root.attribute.values != null) { for (int i = 0; i < root.attribute.values.Length; i++) { if (i > 0) { Console.Write(tabs + "|" + root.attribute + "=" + root.attribute.values[i] + ":"); } else { Console.Write("=" + root.attribute.values[i] + ":"); } TreeNode childNode = root.getBranchChild(root.attribute.values[i]); if (childNode.totalCountOfChilds > 1) { Console.Write(Environment.NewLine); printNode(childNode, "| \t" + tabs); } else { printNode(childNode, "\t" + tabs); } } } }
/*The function adds a node to the Tree*/ public void AddNode(TreeNode treeNode, string ValueName) { int index = tAttribute.indexValue(ValueName); tChilds[index] = treeNode; }
/* The function is used to traverse the decision tree*/ public static void TraverseMain(TreeNode node, string filename, ArrayList attributenames, string filetype, ref DataTable result, int colNumber) { DataTable dt = getDataTable(filename, attributenames); string val = string.Empty; for (int j = 0; j < dt.Rows.Count; j++) { val = string.Empty; Traverse(node, dt.Rows[j], ref val); result.Rows[j][colNumber] = val; } }
/*This function is used to construct the tree amd is called recursively*/ private TreeNode constructTree(DataTable samples, string resultLabel, Attribute[] attributes, string filename, ArrayList attributenames) { if (tuplePositiveTest(samples, resultLabel) == true) /* check if all tuples belong to class label 1*/ return new TreeNode(new Attribute("1")); if (tupleNegativeTest(samples, resultLabel) == true) /* check if all tuples belong to class label 1*/ return new TreeNode(new Attribute("0")); TotalTuples = samples.Rows.Count; resultClass = resultLabel; TotalPositives = countPositiveClass(samples); int mnegative = TotalTuples - TotalPositives; /*Below are the conditions that check when attribute set is empty or tuples are over etc.,*/ if (attributes.Length == 0 && TotalPositives == mnegative) return new TreeNode(new Attribute(getMostCommonValue(samples, resultLabel))); else if (attributes.Length == 0 && TotalPositives == mnegative) return new TreeNode(new Attribute(getMostCommonValue(getDataTable(filename, attributenames), resultLabel))); else if (samples.Rows.Count == 0) return new TreeNode(new Attribute(getMostCommonValue(getDataTable(filename, attributenames), resultLabel))); Entropy = calcEntropy(TotalPositives, TotalTuples - TotalPositives); /*To find the best attribute*/ Attribute bestAttribute = getSplittingAttribute(samples, attributes); if (bestAttribute == null && TotalPositives == mnegative) { return new TreeNode(new Attribute(getMostCommonValue(getDataTable(filename, attributenames), resultLabel))); } else if (bestAttribute == null && TotalPositives != mnegative) { return new TreeNode(new Attribute(getMostCommonValue(samples, resultLabel))); } TreeNode root = new TreeNode(bestAttribute); DataTable aSample = samples.Clone(); bestAttribute.postives = TotalPositives; bestAttribute.negatives = mnegative; /*Loop through all possible attribute values to split based on the above best attribute obtained */ foreach (string value in bestAttribute.values) { aSample.Rows.Clear(); DataRow[] rows = samples.Select(bestAttribute.AttributeName + " = " + "'" + value + "'"); foreach (DataRow row in rows) { aSample.Rows.Add(row.ItemArray); } ArrayList aAttributes = new ArrayList(attributes.Length - 1); for (int i = 0; i < attributes.Length; i++) { if (attributes[i].AttributeName != bestAttribute.AttributeName) aAttributes.Add(attributes[i]); } DecisionID3 dc3 = new DecisionID3(); TreeNode child = dc3.MainTree(aSample, resultLabel, (Attribute[])aAttributes.ToArray(typeof(Attribute)), filename, attributenames); root.AddNode(child, value); } return root; }