Ejemplo n.º 1
0
        private DecisionTree decisionTreeLearning(DataSet ds, ICollection <string> attributeNames, ConstantDecisonTree defaultTree)
        {
            if (ds.size() == 0)
            {
                return(defaultTree);
            }
            if (allExamplesHaveSameClassification(ds))
            {
                return(new ConstantDecisonTree(ds.getExample(0).targetValue()));
            }
            if (attributeNames.Size() == 0)
            {
                return(majorityValue(ds));
            }
            string chosenAttribute = chooseAttribute(ds, attributeNames);

            DecisionTree        tree = new DecisionTree(chosenAttribute);
            ConstantDecisonTree m    = majorityValue(ds);

            ICollection <string> values = ds.getPossibleAttributeValues(chosenAttribute);

            foreach (string v in values)
            {
                DataSet filtered = ds.matchingDataSet(chosenAttribute, v);
                ICollection <string> newAttribs = Util.removeFrom(attributeNames, chosenAttribute);
                DecisionTree         subTree    = decisionTreeLearning(filtered, newAttribs, m);
                tree.addNode(v, subTree);
            }

            return(tree);
        }
        //
        // PRIVATE METHODS
        //

        private DecisionTree DecisionTreeLearning(DataSet ds,
                                                  IList <string> attributeNames, ConstantDecisonTree defaultTree)
        {
            if (ds.Count == 0)
            {
                return(defaultTree);
            }
            if (this.AllExamplesHaveSameClassification(ds))
            {
                return(new ConstantDecisonTree(ds.GetExample(0).TargetValue()));
            }
            if (attributeNames.Count == 0)
            {
                return(this.MajorityValue(ds));
            }
            string chosenAttribute = this.ChooseAttribute(ds, attributeNames);

            var tree = new DecisionTree(chosenAttribute);
            ConstantDecisonTree m = this.MajorityValue(ds);

            IList <string> values = ds.GetPossibleAttributeValues(chosenAttribute);

            foreach (string v in values)
            {
                DataSet        filtered   = ds.MatchingDataSet(chosenAttribute, v);
                IList <string> newAttribs = Util.Util.RemoveFrom(attributeNames,
                                                                 chosenAttribute);
                DecisionTree subTree = this.DecisionTreeLearning(filtered, newAttribs, m);
                tree.AddNode(v, subTree);
            }

            return(tree);
        }