private void Run_Click(object sender, RoutedEventArgs e) { if (isLoad == true) { Attribute hair = new Attribute("HairColor", new string[] { "Black", "Gray", "Silver" }); Attribute height = new Attribute("Height", new string[] { "Short", "Medium", "High" }); Attribute weight = new Attribute("Weight", new string[] { "Light", "Medium", "Heavy" }); Attribute cream = new Attribute("Cream", new string[] { "Yes", "No" }); Attribute[] attributes = new Attribute[] { hair, height, weight, cream }; DataTable samples = datatable; DecisionTree id3 = new DecisionTree(); TreeNode root = id3.mountTree(samples, "Result", attributes); TreeNode root1 = root; var decisiontree = new DecisionTree(); decisiontree.SearchRule(root); RuleID3 = decisiontree.RuleID3; int i = 1; foreach (var rule in RuleID3) { ListRule.Add("Rule [" +i+ "]: IF {" + rule); i++; } lvRule.ItemsSource = ListRule; DecisionTree.printNode(root, " "); tvDecisionTree.Items.Clear(); TreeViewItem item = new TreeViewItem(); item.Header = "Logical Tree"; DumpVisualTree(item, root1); tvDecisionTree.Items.Add(item); item.ExpandSubtree(); txtTree.Text = DecisionTree.TreeList; } else MessageBox.Show("Data must load before run", "Error", MessageBoxButton.OK, MessageBoxImage.Error); }
private TreeNode internalMountTree(DataTable samples, string targetAttribute, Attribute[] attributes) { if (allSamplesPositives(samples, targetAttribute) == true) return new TreeNode(new Attribute(true)); if (allSamplesNegatives(samples, targetAttribute) == true) return new TreeNode(new Attribute(false)); if (attributes.Length == 0) return new TreeNode(new Attribute(getMostCommonValue(samples, targetAttribute))); mTotal = samples.Rows.Count; mTargetAttribute = targetAttribute; mTotalPositives = countTotalPositives(samples); mEntropySet = calcEntropy(mTotalPositives, mTotal - mTotalPositives); Attribute bestAttribute = getBestAttribute(samples, attributes); TreeNode root = new TreeNode(bestAttribute); DataTable aSample = samples.Clone(); 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]); } if (aSample.Rows.Count == 0) { return new TreeNode(new Attribute(getMostCommonValue(aSample, targetAttribute))); } else { DecisionTree dc3 = new DecisionTree(); TreeNode ChildNode = dc3.mountTree(aSample, targetAttribute, (Attribute[])aAttributes.ToArray(typeof(Attribute))); root.AddTreeNode(ChildNode, value); } } return root; }