Exemple #1
0
        private static void SolveEpsilonSvr(SvmProblem prob, SvmParameter param, IList <double> alpha,
                                            SvmSolver.SolutionInfo si)
        {
            int l = prob.Count;

            double[] alpha2     = new double[2 * l];
            double[] linearTerm = new double[2 * l];
            short[]  y          = new short[2 * l];
            int      i;

            for (i = 0; i < l; i++)
            {
                alpha2[i]         = 0;
                linearTerm[i]     = param.p - prob.y[i];
                y[i]              = 1;
                alpha2[i + l]     = 0;
                linearTerm[i + l] = param.p + prob.y[i];
                y[i + l]          = -1;
            }
            SvmSolver s = new SvmSolver();

            s.Solve(2 * l, new SvrQ(prob, param), linearTerm, y, alpha2, param.c, param.c, param.eps, si, param.shrinking);
            double sumAlpha = 0;

            for (i = 0; i < l; i++)
            {
                alpha[i]  = alpha2[i] - alpha2[i + l];
                sumAlpha += Math.Abs(alpha[i]);
            }
            Info("nu = " + sumAlpha / (param.c * l) + "\n");
        }
Exemple #2
0
        private static void SolveOneClass(SvmProblem prob, SvmParameter param, double[] alpha, SvmSolver.SolutionInfo si)
        {
            int l = prob.Count;

            double[] zeros = new double[l];
            short[]  ones  = new short[l];
            int      i;
            int      n = (int)(param.nu * prob.Count);       // # of alpha's at upper bound

            for (i = 0; i < n; i++)
            {
                alpha[i] = 1;
            }
            if (n < prob.Count)
            {
                alpha[n] = param.nu * prob.Count - n;
            }
            for (i = n + 1; i < l; i++)
            {
                alpha[i] = 0;
            }
            for (i = 0; i < l; i++)
            {
                zeros[i] = 0;
                ones[i]  = 1;
            }
            SvmSolver s = new SvmSolver();

            s.Solve(l, new OneClassQ(prob, param), zeros, ones, alpha, 1.0, 1.0, param.eps, si, param.shrinking);
        }
Exemple #3
0
        private static void SolveCSvc(SvmProblem prob, SvmParameter param, double[] alpha, SvmSolver.SolutionInfo si,
                                      double cp, double cn)
        {
            int l = prob.Count;

            double[] minusOnes = new double[l];
            short[]  y         = new short[l];
            int      i;

            for (i = 0; i < l; i++)
            {
                alpha[i]     = 0;
                minusOnes[i] = -1;
                if (prob.y[i] > 0)
                {
                    y[i] = +1;
                }
                else
                {
                    y[i] = -1;
                }
            }
            SvmSolver s = new SvmSolver();

            s.Solve(l, new SvcQ(prob, param, y), minusOnes, y, alpha, cp, cn, param.eps, si, param.shrinking);
            double sumAlpha = 0;

            for (i = 0; i < l; i++)
            {
                sumAlpha += alpha[i];
            }
            if (cp == cn)
            {
                Info("nu = " + sumAlpha / (cp * prob.Count) + "\n");
            }
            for (i = 0; i < l; i++)
            {
                alpha[i] *= y[i];
            }
        }
Exemple #4
0
 private static void SolveOneClass(SvmProblem prob, SvmParameter param, double[] alpha, SvmSolver.SolutionInfo si)
 {
     int l = prob.Count;
     double[] zeros = new double[l];
     short[] ones = new short[l];
     int i;
     int n = (int) (param.nu*prob.Count); // # of alpha's at upper bound
     for (i = 0; i < n; i++){
         alpha[i] = 1;
     }
     if (n < prob.Count){
         alpha[n] = param.nu*prob.Count - n;
     }
     for (i = n + 1; i < l; i++){
         alpha[i] = 0;
     }
     for (i = 0; i < l; i++){
         zeros[i] = 0;
         ones[i] = 1;
     }
     SvmSolver s = new SvmSolver();
     s.Solve(l, new OneClassQ(prob, param), zeros, ones, alpha, 1.0, 1.0, param.eps, si, param.shrinking);
 }
Exemple #5
0
        private static void SolveEpsilonSvr(SvmProblem prob, SvmParameter param, IList<double> alpha,
			SvmSolver.SolutionInfo si)
        {
            int l = prob.Count;
            double[] alpha2 = new double[2*l];
            double[] linearTerm = new double[2*l];
            short[] y = new short[2*l];
            int i;
            for (i = 0; i < l; i++){
                alpha2[i] = 0;
                linearTerm[i] = param.p - prob.y[i];
                y[i] = 1;
                alpha2[i + l] = 0;
                linearTerm[i + l] = param.p + prob.y[i];
                y[i + l] = -1;
            }
            SvmSolver s = new SvmSolver();
            s.Solve(2*l, new SvrQ(prob, param), linearTerm, y, alpha2, param.c, param.c, param.eps, si, param.shrinking);
            double sumAlpha = 0;
            for (i = 0; i < l; i++){
                alpha[i] = alpha2[i] - alpha2[i + l];
                sumAlpha += Math.Abs(alpha[i]);
            }
            Info("nu = " + sumAlpha/(param.c*l) + "\n");
        }
Exemple #6
0
        private static void SolveCSvc(SvmProblem prob, SvmParameter param, double[] alpha, SvmSolver.SolutionInfo si,
			double cp, double cn)
        {
            int l = prob.Count;
            double[] minusOnes = new double[l];
            short[] y = new short[l];
            int i;
            for (i = 0; i < l; i++){
                alpha[i] = 0;
                minusOnes[i] = -1;
                if (prob.y[i] > 0){
                    y[i] = +1;
                } else{
                    y[i] = -1;
                }
            }
            SvmSolver s = new SvmSolver();
            s.Solve(l, new SvcQ(prob, param, y), minusOnes, y, alpha, cp, cn, param.eps, si, param.shrinking);
            double sumAlpha = 0;
            for (i = 0; i < l; i++){
                sumAlpha += alpha[i];
            }
            if (cp == cn){
                Info("nu = " + sumAlpha/(cp*prob.Count) + "\n");
            }
            for (i = 0; i < l; i++){
                alpha[i] *= y[i];
            }
        }