/// <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, TreeAttributeCollection attributes) { if (allSamplesArePositive(samples, targetAttribute) == true) { return(new TreeNode(new OutcomeTreeAttribute(true))); } if (allSamplesAreNegative(samples, targetAttribute) == true) { return(new TreeNode(new OutcomeTreeAttribute(false))); } if (attributes.Count == 0) { return(new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(samples, targetAttribute)))); } mTotal = samples.Rows.Count; mTargetAttribute = targetAttribute; mTotalPositives = countTotalPositives(samples); mEntropySet = getCalculatedEntropy(mTotalPositives, mTotal - mTotalPositives); TreeAttribute bestAttribute = getBestAttribute(samples, attributes); TreeNode root = new TreeNode(bestAttribute); if (bestAttribute == null) { return(root); } DataTable aSample = samples.Clone(); foreach (string value in bestAttribute.PossibleValues) { // 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 TreeAttributeCollection aAttributes = new TreeAttributeCollection(); //ArrayList aAttributes = new ArrayList(attributes.Count - 1); for (int i = 0; i < attributes.Count; 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 OutcomeTreeAttribute(getMostCommonValue(aSample, targetAttribute)))); } else { DecisionTree dc3 = new DecisionTree(); TreeNode ChildNode = dc3.mountTree(aSample, targetAttribute, aAttributes); root.AddTreeNode(ChildNode, value); } } return(root); }
/// <summary> /// Przechodzenie przez drzewo decyzyjne w oparciu o wy¿ej przedstawione próbki /// </summary> /// <param name="samples">Tabela z próbkami nale¿y przeniesc do drzewa</param> /// <param name="targetAttribute"> Nazwa kolumny tabeli, która ma wartoœæ true lub false, aby zweryfikowaæ dana próbke</param> private TreeNode internalMountTree(DataTable samples, string targetAttribute, TreeAttributeCollection attributes) { if (allSamplesArePositive(samples, targetAttribute) == true) { return(new TreeNode(new OutcomeTreeAttribute(true))); } if (allSamplesAreNegative(samples, targetAttribute) == true) { return(new TreeNode(new OutcomeTreeAttribute(false))); } if (attributes.Count == 0) { return(new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(samples, targetAttribute)))); } mTotal = samples.Rows.Count; mTargetAttribute = targetAttribute; mTotalPositives = countTotalPositives(samples); mEntropySet = getCalculatedEntropy(mTotalPositives, mTotal - mTotalPositives); TreeAttribute bestAttribute = getBestAttribute(samples, attributes); TreeNode root = new TreeNode(bestAttribute); if (bestAttribute == null) { return(root); } DataTable aSample = samples.Clone(); foreach (string value in bestAttribute.PossibleValues) { // Zaznacz wszystkie elementy z wartoœci¹ tego atrybutu aSample.Rows.Clear(); DataRow[] rows = samples.Select(bestAttribute.AttributeName + " = " + "'" + value + "'"); foreach (DataRow row in rows) { aSample.Rows.Add(row.ItemArray); } // Zaznacz wszystkie elementy z wartoœci¹ tego atrybutu // Utwórz now¹ listê atrybutów, najmniej wystepujacy atrybut jest najlepszym atrybutem TreeAttributeCollection aAttributes = new TreeAttributeCollection(); for (int i = 0; i < attributes.Count; i++) { if (attributes[i].AttributeName != bestAttribute.AttributeName) { aAttributes.Add(attributes[i]); } } if (aSample.Rows.Count == 0) { return(new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(aSample, targetAttribute)))); } else { DecisionTree dc3 = new DecisionTree(); TreeNode ChildNode = dc3.mountTree(aSample, targetAttribute, aAttributes); root.AddTreeNode(ChildNode, value); } } return(root); }