Beispiel #1
0
        protected override void DoTrain()
        {
            base.DoTrain();

            var classes = Classes.ToList();

            for (int i = 0; i < classes.Count; i++)
            {
                var any = classes.Any(c => (int)c.Value == i);
                if (!any)
                {
                    throw new MLException(string.Format("Class values must be enumerated from 0 to {0}", classes.Count));
                }
            }

            m_ClassHist  = new int[classes.Count];
            m_PriorProbs = new double[classes.Count];
            m_DataCount  = TrainingSample.Count;
            m_DataDim    = TrainingSample.GetDimension();

            foreach (var pData in TrainingSample)
            {
                var cls = pData.Value;
                m_ClassHist[cls.Value] += 1;
            }

            foreach (var cls in classes)
            {
                var penalty = (ClassLosses == null) ? 1 : ClassLosses[cls.Value];
                m_PriorProbs[cls.Value] = Math.Log(penalty * m_ClassHist[cls.Value] / (double)m_DataCount);
            }

            TrainImpl();
        }
Beispiel #2
0
        /// <summary>
        /// Estimated proximity of given point to given classes
        /// </summary>
        public override double CalculateClassScore(double[] obj, Class cls)
        {
            var dim = TrainingSample.GetDimension();
            var p   = PriorProbs[cls.Value];
            var ds  = m_DistributionParameters[cls.Value];

            foreach (var pData in TrainingSample.Where(d => d.Value.Equals(cls)))
            {
                var data = pData.Key;

                for (int i = 0; i < dim; i++)
                {
                    m_Distribution.Params = ds[i];
                    var value = m_Distribution.LogValue(obj[i]);

                    p += value;
                }
            }

            return(p);
        }