Example #1
0
        /// <summary>
        /// Leave-one-out optimization
        /// </summary>
        public void Train_LOO(double hMin, double hMax, double step)
        {
            var hOpt      = double.MaxValue;
            var minErrCnt = int.MaxValue;

            for (double h = hMin; h <= hMax; h += step)
            {
                var errCnt = 0;
                m_H = h;

                for (int i = 0; i < TrainingSample.Count; i++)
                {
                    var pData = TrainingSample.ElementAt(i);
                    using (var mask = this.ApplySampleMask((p, c, idx) => idx != i))
                    {
                        var cls = this.Classify(pData.Key);
                        if (cls != pData.Value)
                        {
                            errCnt++;
                        }
                    }
                }

                if (errCnt < minErrCnt)
                {
                    minErrCnt = errCnt;
                    hOpt      = h;
                }
            }

            m_H = hOpt;
        }
        /// <summary>
        /// Leave-one-out optimization
        /// </summary>
        public void Train_LOO(int?minK = null, int?maxK = null)
        {
            if (!minK.HasValue || minK.Value < 1)
            {
                minK = 1;
            }
            if (!maxK.HasValue || maxK.Value > TrainingSample.Count)
            {
                maxK = TrainingSample.Count;
            }

            var kOpt      = int.MaxValue;
            var minErrCnt = int.MaxValue;

            for (int k = minK.Value; k <= maxK.Value; k++)
            {
                var errCnt = 0;
                m_K = k;

                for (int i = 0; i < TrainingSample.Count; i++)
                {
                    var pData = TrainingSample.ElementAt(i);
                    using (var mask = this.ApplySampleMask((p, c, idx) => idx != i))
                    {
                        var cls = this.Classify(pData.Key);
                        if (cls != pData.Value)
                        {
                            errCnt++;
                        }
                    }
                }

                if (errCnt < minErrCnt)
                {
                    minErrCnt = errCnt;
                    kOpt      = k;
                }
            }

            m_K = kOpt;
        }