Пример #1
0
        private static void SolveNuSvr(SvmProblem prob, SvmParameter param, IList <double> alpha, SvmSolver.SolutionInfo si)
        {
            int    l = prob.Count;
            double c = param.c;

            double[] alpha2     = new double[2 * l];
            double[] linearTerm = new double[2 * l];
            short[]  y          = new short[2 * l];
            int      i;
            double   sum = c * param.nu * l / 2;

            for (i = 0; i < l; i++)
            {
                alpha2[i]         = alpha2[i + l] = Math.Min(sum, c);
                sum              -= alpha2[i];
                linearTerm[i]     = -prob.y[i];
                y[i]              = 1;
                linearTerm[i + l] = prob.y[i];
                y[i + l]          = -1;
            }
            SvmSolverNu s = new SvmSolverNu();

            s.Solve(2 * l, new SvrQ(prob, param), linearTerm, y, alpha2, c, c, param.eps, si, param.shrinking);
            Info("epsilon = " + (-si.r) + "\n");
            for (i = 0; i < l; i++)
            {
                alpha[i] = alpha2[i] - alpha2[i + l];
            }
        }
Пример #2
0
        private static void SolveNuSvc(SvmProblem prob, SvmParameter param, double[] alpha, SvmSolver.SolutionInfo si)
        {
            int    i;
            int    l  = prob.Count;
            double nu = param.nu;

            short[] y = new short[l];
            for (i = 0; i < l; i++)
            {
                if (prob.y[i] > 0)
                {
                    y[i] = +1;
                }
                else
                {
                    y[i] = -1;
                }
            }
            double sumPos = nu * l / 2;
            double sumNeg = nu * l / 2;

            for (i = 0; i < l; i++)
            {
                if (y[i] == +1)
                {
                    alpha[i] = Math.Min(1.0, sumPos);
                    sumPos  -= alpha[i];
                }
                else
                {
                    alpha[i] = Math.Min(1.0, sumNeg);
                    sumNeg  -= alpha[i];
                }
            }
            double[] zeros = new double[l];
            for (i = 0; i < l; i++)
            {
                zeros[i] = 0;
            }
            SvmSolverNu s = new SvmSolverNu();

            s.Solve(l, new SvcQ(prob, param, y), zeros, y, alpha, 1.0, 1.0, param.eps, si, param.shrinking);
            double r = si.r;

            Info("C = " + 1 / r + "\n");
            for (i = 0; i < l; i++)
            {
                alpha[i] *= y[i] / r;
            }
            si.rho        /= r;
            si.obj        /= (r * r);
            si.upperBoundP = 1 / r;
            si.upperBoundN = 1 / r;
        }
Пример #3
0
 private static void SolveNuSvr(SvmProblem prob, SvmParameter param, IList<double> alpha, SvmSolver.SolutionInfo si)
 {
     int l = prob.Count;
     double c = param.c;
     double[] alpha2 = new double[2*l];
     double[] linearTerm = new double[2*l];
     short[] y = new short[2*l];
     int i;
     double sum = c*param.nu*l/2;
     for (i = 0; i < l; i++){
         alpha2[i] = alpha2[i + l] = Math.Min(sum, c);
         sum -= alpha2[i];
         linearTerm[i] = -prob.y[i];
         y[i] = 1;
         linearTerm[i + l] = prob.y[i];
         y[i + l] = -1;
     }
     SvmSolverNu s = new SvmSolverNu();
     s.Solve(2*l, new SvrQ(prob, param), linearTerm, y, alpha2, c, c, param.eps, si, param.shrinking);
     Info("epsilon = " + (-si.r) + "\n");
     for (i = 0; i < l; i++){
         alpha[i] = alpha2[i] - alpha2[i + l];
     }
 }
Пример #4
0
 private static void SolveNuSvc(SvmProblem prob, SvmParameter param, double[] alpha, SvmSolver.SolutionInfo si)
 {
     int i;
     int l = prob.Count;
     double nu = param.nu;
     short[] y = new short[l];
     for (i = 0; i < l; i++){
         if (prob.y[i] > 0){
             y[i] = +1;
         } else{
             y[i] = -1;
         }
     }
     double sumPos = nu*l/2;
     double sumNeg = nu*l/2;
     for (i = 0; i < l; i++){
         if (y[i] == +1){
             alpha[i] = Math.Min(1.0, sumPos);
             sumPos -= alpha[i];
         } else{
             alpha[i] = Math.Min(1.0, sumNeg);
             sumNeg -= alpha[i];
         }
     }
     double[] zeros = new double[l];
     for (i = 0; i < l; i++){
         zeros[i] = 0;
     }
     SvmSolverNu s = new SvmSolverNu();
     s.Solve(l, new SvcQ(prob, param, y), zeros, y, alpha, 1.0, 1.0, param.eps, si, param.shrinking);
     double r = si.r;
     Info("C = " + 1/r + "\n");
     for (i = 0; i < l; i++){
         alpha[i] *= y[i]/r;
     }
     si.rho /= r;
     si.obj /= (r*r);
     si.upperBoundP = 1/r;
     si.upperBoundN = 1/r;
 }