Пример #1
0
        /// <summary>
        /// Retorna o melhor atributo.
        /// </summary>
        /// <param name="attributes">Um vetor com os atributos</param>
        /// <returns>Retorna o que tiver maior ganho</returns>
        private TreeAttribute getBestAttribute(DataTable samples, TreeAttributeCollection attributes)
        {
            double        maxGain = 0.0;
            TreeAttribute result  = null;

            foreach (TreeAttribute attribute in attributes)
            {
                double aux = gain(samples, attribute);
                if (aux > maxGain)
                {
                    maxGain = aux;
                    result  = attribute;
                }
            }
            return(result);
        }
Пример #2
0
        public TreeAttributeCollection GetValidAttributeCollection()
        {
            TreeAttributeCollection returnCollection = new TreeAttributeCollection();

            foreach (DataColumn column in this.Columns)
            {
                TreeAttribute currentAttribute = new TreeAttribute(column.ColumnName, GetValuesFromColumn(column.ColumnName));

                if (returnCollection.ContainsAttribute(currentAttribute) || currentAttribute.AttributeName.ToUpper().Trim() == "RESULT")
                {
                    continue;
                }
                returnCollection.Add(currentAttribute);
            }
            return(returnCollection);
        }
Пример #3
0
 /// <summary>
 /// Inicjuje nowe wyst¹pienie TreeNode
 /// </summary>
 /// <param name="attribute">Atrybut klasy decyzji</param>
 public TreeNode(TreeAttribute attribute)
 {
     if (attribute != null && attribute.PossibleValues != null)
     {
         _children = new TreeNodeCollection();
         for (int i = 0; i < attribute.PossibleValues.Count; i++)
         {
             _children.Add(null);
         }
     }
     else
     {
         _children = new TreeNodeCollection();
         _children.Add(null);
     }
     _attribute = attribute;
 }
Пример #4
0
        /// <summary>
        /// Calcula o ganho de um atributo
        /// </summary>
        /// <param name="attribute">Atributo a ser calculado</param>
        /// <returns>O ganho do atributo</returns>
        private double gain(DataTable samples, TreeAttribute attribute)
        {
            PossibleValueCollection values = attribute.PossibleValues;
            double sum = 0.0;

            for (int i = 0; i < values.Count; i++)
            {
                int positives, negatives;

                positives = negatives = 0;

                getValuesToAttribute(samples, attribute, values[i], out positives, out negatives);

                double entropy = getCalculatedEntropy(positives, negatives);
                sum += -(double)(positives + negatives) / mTotal * entropy;
            }
            return(mEntropySet + sum);
        }
Пример #5
0
        /// <summary>
        /// Varre tabela de amostras verificando um atributo e se o resultado é positivo ou negativo
        /// </summary>
        /// <param name="samples">DataTable com as amostras</param>
        /// <param name="attribute">Atributo a ser pesquisado</param>
        /// <param name="value">valor permitido para o atributo</param>
        /// <param name="positives">Conterá o nro de todos os atributos com o valor determinado com resultado positivo</param>
        /// <param name="negatives">Conterá o nro de todos os atributos com o valor determinado com resultado negativo</param>
        private void getValuesToAttribute(DataTable samples, TreeAttribute attribute, string value, out int positives, out int negatives)
        {
            positives = 0;
            negatives = 0;

            foreach (DataRow aRow in samples.Rows)
            {
                ///To do:   Figure out if this is correct - it looks bad
                if (((string)aRow[attribute.AttributeName] == value))
                {
                    if (aRow[mTargetAttribute].ToString().Trim().ToUpper() == "TRUE")
                    {
                        positives++;
                    }
                    else
                    {
                        negatives++;
                    }
                }
            }
        }
Пример #6
0
        /// <summary>
        /// Metoda usuwajaca przyk³adow¹ tabelê sprawdzaj¹c atrybut, a jeœli wynik jest dodatni lub ujemny
        /// </summary>
        /// <param name="samples">DataTable z próbkami</param>
        /// <param name="attribute">Atrybut wyszukaiwania</param>
        /// <param name="value">wartoϾ artybutu</param>
        /// <param name="positives">NRO bêdzie zawiera³ wszystkie atrybuty z wartoœci okreœlonej jako dodatnie</param>
        /// <param name="negatives">NRO bêdzie zawiera³ wszystkie atrybuty z wartoœci okreœlonej jako ujemne</param>
        private void getValuesToAttribute(DataTable samples, TreeAttribute attribute, string value, out int positives, out int negatives)
        {
            positives = 0;
            negatives = 0;

            foreach (DataRow aRow in samples.Rows)
            {
                ///Do zrobienia: dowiedzieæ siê, czy jest to ok - bo wygl¹da Ÿle
                if (((string)aRow[attribute.AttributeName] == value))
                {
                    if (aRow[mTargetAttribute].ToString().Trim().ToUpper() == "TRUE")
                    {
                        positives++;
                    }
                    else
                    {
                        negatives++;
                    }
                }
            }
        }
Пример #7
0
        /// <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);
        }
Пример #8
0
        /// <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);
        }