예제 #1
0
 public void EntropyH_Values()
 {
     Assert.AreEqual(0.0F, GeneralUtils.EntropyH(0), EPS);
     Assert.AreEqual(0.33219280948874F, GeneralUtils.EntropyH(0.1F), EPS);
     Assert.AreEqual(0.46438561897747F, GeneralUtils.EntropyH(0.2F), EPS);
     Assert.AreEqual(0.52108967824986F, GeneralUtils.EntropyH(0.3F), EPS);
     Assert.AreEqual(0.52877123795494F, GeneralUtils.EntropyH(0.4F), EPS);
     Assert.AreEqual(0.5F, GeneralUtils.EntropyH(0.5F), EPS);
     Assert.AreEqual(0.44217935649972F, GeneralUtils.EntropyH(0.6F), EPS);
     Assert.AreEqual(0.36020122098083F, GeneralUtils.EntropyH(0.7F), EPS);
     Assert.AreEqual(0.25754247590989F, GeneralUtils.EntropyH(0.8F), EPS);
     Assert.AreEqual(0.13680278410054F, GeneralUtils.EntropyH(0.9F), EPS);
     Assert.AreEqual(0.0F, GeneralUtils.EntropyH(1.0F), EPS);
 }
예제 #2
0
        public override double Calculate(Predicate <TObj> pattern, ClassifiedSample <TObj> sample)
        {
            var clsInfos = new Dictionary <Class, ClassBag>();
            int p        = 0;
            var l        = sample.Count;

            foreach (var pData in sample)
            {
                var      cls = pData.Value;
                ClassBag bag;
                if (!clsInfos.TryGetValue(cls, out bag))
                {
                    bag = new ClassBag();
                    clsInfos.Add(cls, bag);
                }

                bag.Pc += 1;
                if (pattern(pData.Key))
                {
                    p      += 1;
                    bag.pc += 1;
                }
            }

            var result = 0.0D;

            foreach (var cData in clsInfos)
            {
                var bag = cData.Value;
                result += GeneralUtils.EntropyH(bag.Pc / l);
                result += (p == 0) ? 0 : -(double)p / l * GeneralUtils.EntropyH(bag.pc / p);
                result += (p == l) ? 0 : -(double)(l - p) / l * GeneralUtils.EntropyH((bag.Pc - bag.pc) / (l - p));
            }

            return(result);
        }