Exemple #1
0
        public override void Algorithm()
        {
            SupportVectors.Clear();
            SupportVectors.Capacity = 0;
            // Save support vectors indexes
            for (int i = 0; i < Solver.lagrangians.Length; ++i)
            {
                if (Solver.lagrangians[i] > 0)
                {
                    SupportVectors.Add(i);
                }
            }

            foreach (var i in SupportVectors)
            {
                foreach (var j in SupportVectors)
                {
                    Lambda += Labels[i] * Labels[j] * Solver.lagrangians[i] * Solver.lagrangians[j] * KernelMatrix[i, j];
                }
            }

            int chosen = SupportVectors.FirstOrDefault();

            B = Labels[chosen] * Lambda;

            foreach (var i in SupportVectors)
            {
                B -= Solver.lagrangians[i] * Labels[i] * KernelMatrix[chosen, i];
            }

            Margin = Math.Sqrt(Lambda);
        }
Exemple #2
0
        public override void Algorithm()
        {
            SupportVectors.Clear();
            SupportVectors.Capacity = 0;
            // Save support vectors indexes
            for (int i = 0; i < Solver.lagrangians.Length; ++i)
            {
                if (Solver.lagrangians[i] > 0)
                {
                    SupportVectors.Add(i);
                }
            }

            int indexi = -1, indexj = -1;

            foreach (var i in SupportVectors)
            {
                if ((-1.0) / (Nu * Values.Length) < Solver.lagrangians[i] * Labels[i] && Solver.lagrangians[i] * Labels[i] < 0)
                {
                    indexi = i;
                }
                if (0 < Solver.lagrangians[i] * Labels[i] && Solver.lagrangians[i] * Labels[i] < 1.0 / (Nu * Values.Length))
                {
                    indexj = i;
                }

                foreach (var j in SupportVectors)
                {
                    Lambda += Labels[i] * Labels[j] * Solver.lagrangians[i] * Solver.lagrangians[j] * KernelMatrix[i, j];
                }
            }

            Lambda = Math.Sqrt(Lambda) / 2.0;

            double left = 0, right = 0;

            foreach (var k in SupportVectors)
            {
                left  += Solver.lagrangians[k] * Labels[k] * KernelMatrix[k, indexi];
                right += Solver.lagrangians[k] * Labels[k] * KernelMatrix[k, indexj];
            }

            B      = -Lambda * (left + right);
            Margin = 2 * Lambda * right + B;
        }