Exemplo n.º 1
0
        /// <summary>
        /// Generate decision tree via ID3 algorithm
        /// </summary>
        public void Train(IEnumerable <Predicate <TObj> > patterns, IInformativityIndex <TObj> informativity)
        {
            if (patterns == null || !patterns.Any())
            {
                throw new MLException("Patterns are empty or null");
            }
            if (informativity == null)
            {
                throw new MLException("Informativity is null");
            }

            var root = trainID3Core(patterns, TrainingSample, informativity);

            m_Result = new DecisionTree <TObj>(root);
        }
Exemplo n.º 2
0
        private DecisionNode <TObj> trainID3Core(IEnumerable <Predicate <TObj> > patterns, ClassifiedSample <TObj> sample, IInformativityIndex <TObj> informativity)
        {
            if (!sample.Any())
            {
                throw new MLException("Empty sample");
            }

            var cls = sample.First().Value;

            if (sample.All(kvp => kvp.Value.Equals(cls)))
            {
                return(new LeafNode <TObj>(cls));
            }

            var pattern   = informativity.Max(patterns, sample);
            var negSample = new ClassifiedSample <TObj>();
            var posSample = new ClassifiedSample <TObj>();

            foreach (var pData in sample)
            {
                if (pattern(pData.Key))
                {
                    posSample.Add(pData.Key, pData.Value);
                }
                else
                {
                    negSample.Add(pData.Key, pData.Value);
                }
            }

            if (!negSample.Any() || !posSample.Any())
            {
                var majorClass = sample.GroupBy(pd => pd.Value)
                                 .Select(g => new KeyValuePair <Class, int>(g.Key, g.Count()))
                                 .OrderByDescending(c => c.Value)
                                 .First();
                return(new LeafNode <TObj>(majorClass.Key));
            }

            var node    = new InnerNode <TObj>(pattern);
            var negNode = trainID3Core(patterns, negSample, informativity);
            var posNode = trainID3Core(patterns, posSample, informativity);

            node.SetNegativeNode(negNode);
            node.SetPositiveNode(posNode);

            return(node);
        }