Пример #1
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);
        }
Пример #2
0
        /// <summary>
        /// Sets up a decision tree based on samples submitted
        /// </summary>
        /// <param name="samples">Table with samples that will be provided for mounting the tree </param>
        /// <param name="targetAttribute"> Name column of the table that otherwise has the value true or false to
        /// Validate or not a sample</param>
        /// <returns>The root of the decision tree mounted </returns></returns?>
        private TreeNode internalMountTree(DataTable samples, string targetAttribute, TreeAttributeCollection attributes)
        {
            if (allSamplesAreUniform(samples, targetAttribute) == true)
            {
                return(new TreeNode(new OutcomeTreeAttribute(getUniformRefValue(samples, targetAttribute))));
            }
            //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);
            }
            PossibleValueCollection bestAttrValues = bestAttribute.PossibleValues;
            bool continousSet = isContinousSet(bestAttrValues);

            //DataTable aSample = samples.Clone();
            if (continousSet)
            {
                string value = bestSplitValue(samples, bestAttribute);
                {
                    DataTable aSample = samples.Clone();
                    //First Below then Above
                    DataRow[] rows;
                    string    cond = bestAttribute.AttributeName + " <= " + "" + value + "";
                    rows = samples.Select(cond);

                    aSample.Rows.Clear();
                    foreach (DataRow row in rows)
                    {
                        aSample.Rows.Add(row.ItemArray);
                        Console.WriteLine(" SPLIT {0} ROW:", cond);
                        foreach (DataColumn myCol in samples.Columns)
                        {
                            Console.WriteLine("  " + row[myCol]);
                        }
                    }
                    // Create a new attribute list unless the attribute which is the current best attribute
                    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]);
                        }
                    }
                    //Recycle the best continous attribute if there are others
                    if (aAttributes.Count > 0)
                    {
                        aAttributes.Add(bestAttribute);
                    }

                    // Create a new attribute list unless the attribute which is the current best attribute

                    if (rows.Length == 0)
                    {
                        //return new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(aSample, targetAttribute)));
                        return(new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(samples, targetAttribute))));
                    }
                    else
                    {
                        DecisionTree dc3       = new DecisionTree();
                        TreeNode     ChildNode = dc3.mountTree(aSample, targetAttribute, aAttributes);
                        root.AddTreeNode(ChildNode, value, "leq");
                    }
                }
                {
                    DataTable aSample = samples.Clone();
                    DataRow[] rows2;
                    string    cond = bestAttribute.AttributeName + " > " + "" + value + "";
                    rows2 = samples.Select(cond);

                    aSample.Rows.Clear();
                    foreach (DataRow row in rows2)
                    {
                        aSample.Rows.Add(row.ItemArray);
                        Console.WriteLine(" SPLIT {0} ROW:", cond);
                        foreach (DataColumn myCol in samples.Columns)
                        {
                            Console.WriteLine("  " + row[myCol]);
                        }
                    }
                    // Create a new attribute list unless the attribute which is the current best attribute
                    TreeAttributeCollection aAttributes2 = new TreeAttributeCollection();
                    //ArrayList aAttributes = new ArrayList(attributes.Count - 1);
                    for (int i = 0; i < attributes.Count; i++)
                    {
                        if (attributes[i].AttributeName != bestAttribute.AttributeName)
                        {
                            aAttributes2.Add(attributes[i]);
                        }
                    }
                    //Recycle the best continous attribute if there are others
                    if (aAttributes2.Count > 0)
                    {
                        aAttributes2.Add(bestAttribute);
                    }

                    // Create a new attribute list unless the attribute which is the current best attribute

                    if (rows2.Length == 0)
                    {
                        //return new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(aSample, targetAttribute)));
                        return(new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(samples, targetAttribute))));
                    }
                    else
                    {
                        DecisionTree dc3       = new DecisionTree();
                        TreeNode     ChildNode = dc3.mountTree(aSample, targetAttribute, aAttributes2);
                        root.AddTreeNode(ChildNode, value, "gt");
                    }
                }
            }
            else
            {
                DataTable aSample = samples.Clone();
                foreach (string value in bestAttribute.PossibleValues)
                {
                    // Select all elements with the value of this attribute
                    aSample.Rows.Clear();

                    DataRow[] rows;

                    rows = samples.Select(bestAttribute.AttributeName + " = " + "'" + value + "'");

                    foreach (DataRow row in rows)
                    {
                        aSample.Rows.Add(row.ItemArray);
                    }
                    // Select all elements with the value of this attribute

                    // Create a new attribute list unless the attribute which is the current best attribute
                    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]);
                        }
                    }
                    // Create a new attribute list unless the attribute which is the current best attribute

                    if (aSample.Rows.Count == 0)
                    {
                        //return new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(aSample, targetAttribute)));
                        return(new TreeNode(new OutcomeTreeAttribute(getMostCommonValue(samples, targetAttribute))));
                    }
                    else
                    {
                        DecisionTree dc3       = new DecisionTree();
                        TreeNode     ChildNode = dc3.mountTree(aSample, targetAttribute, aAttributes);
                        root.AddTreeNode(ChildNode, value, "eq");
                    }
                }
            }


            return(root);
        }