static void Main(string[] args) { var parents = new Attribute("parents ", new[] { "usual", "pretentious", "great_pret", }); var has_nurs = new Attribute("has_nurs ", new[] { "proper", "less_proper", "improper", "critical", "very_crit", }); var form = new Attribute("form ", new[] { "complete", "completed", "incomplete", "foster", }); var children = new Attribute("children ", new[] { "1", "2", "3", "more", }); var housing = new Attribute("housing ", new[] { "convenient", "less_conv", "critical", }); var finance = new Attribute("finance ", new[] { "convenient", "inconv", }); var social = new Attribute("social ", new[] { "nonprob", "slightly_prob", "problematic", }); var health = new Attribute("health ", new[] { "recommended", "priority", "not_recom" }); _attributes = new Attribute[] { parents, has_nurs, form, children, housing, finance, social, health }; DataTable samples = getDataTable(); DecisionTreeID3 id3 = new DecisionTreeID3(); TreeNode root = id3.mountTree(samples, cat, _attributes); printNode(root, ""); }
/// <summary> /// Monta uma árvore de decisão baseado nas amostragens apresentadas /// </summary> /// <param name="samples">Tabela com as amostragens que serão apresentadas para a montagem da árvore</param> /// <param name="targetAttribute">Nome da coluna da tabela que possue o valor true ou false para /// validar ou não uma amostragem</param> /// <returns>A raiz da árvore de decisão montada</returns></returns?> 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) { // Seleciona todas os elementos com o valor deste atributo aSample.Rows.Clear(); DataRow[] rows = samples.Select(bestAttribute.AttributeName + " = " + "'" + value + "'"); foreach (DataRow row in rows) { aSample.Rows.Add(row.ItemArray); } // Seleciona todas os elementos com o valor deste atributo // Cria uma nova lista de atributos menos o atributo corrente que é o melhor atributo 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]); } } // Cria uma nova lista de atributos menos o atributo corrente que é o melhor atributo if (aSample.Rows.Count == 0) { return(new TreeNode(new Attribute(getMostCommonValue(aSample, targetAttribute)))); } else { DecisionTreeID3 dc3 = new DecisionTreeID3(); TreeNode ChildNode = dc3.mountTree(aSample, targetAttribute, (Attribute[])aAttributes.ToArray(typeof(Attribute))); root.AddTreeNode(ChildNode, value); } } return(root); }