예제 #1
0
        /// <summary>
        /// Encode the Encog dataset.
        /// </summary>
        /// <param name="training">The training data.</param>
        /// <param name="outputIndex">The ideal element to use, this is necessary becase SVM's have
        /// only a single output.</param>
        /// <returns>The SVM problem.</returns>
        public static svm_problem Encode(INeuralDataSet training, int outputIndex)
        {
            svm_problem result = new svm_problem();

            result.l = (int)ObtainTrainingLength(training);

            result.y = new double[result.l];
            result.x = new svm_node[result.l][];

            int elementIndex = 0;

            foreach (INeuralDataPair pair in training)
            {
                INeuralData input = pair.Input;
                INeuralData output = pair.Ideal;
                result.x[elementIndex] = new svm_node[input.Count];

                for (int i = 0; i < input.Count; i++)
                {
                    result.x[elementIndex][i] = new svm_node();
                    result.x[elementIndex][i].index = i + 1;
                    result.x[elementIndex][i].value_Renamed = input[i];
                }

                result.y[elementIndex] = output[outputIndex];

                elementIndex++;
            }

            return result;
        }
예제 #2
0
 internal xa68541a68dd6f460(svm_problem prob, svm_parameter param, sbyte[] y_)
     : base(prob.l, prob.x, param)
 {
     this.x1e218ceaee1bb583 = new sbyte[y_.Length];
     y_.CopyTo(this.x1e218ceaee1bb583, 0);
     this.x1f31bf6ca58166a1 = new xb730a77005d16cc1(prob.l, (int) (param.cache_size * 1048576.0));
 }
예제 #3
0
 internal xfbfe48e5ee40f893(svm_problem prob, svm_parameter param)
     : base(prob.l, prob.x, param)
 {
     int num;
     int num2;
     if (((uint) num) > uint.MaxValue)
     {
         goto Label_005C;
     }
     this.x9fc3ee03a439f6f0 = prob.l;
     if ((((uint) num) - ((uint) num2)) > uint.MaxValue)
     {
         if ((((uint) num2) + ((uint) num)) > uint.MaxValue)
         {
             goto Label_0115;
         }
     }
     else
     {
         goto Label_0115;
     }
     Label_0024:
     this.x5cafa8d49ea71ea1[num2] = new float[2 * this.x9fc3ee03a439f6f0];
     num2++;
     Label_003D:
     if (num2 < 2)
     {
         goto Label_0024;
     }
     this.xafb0a999075e2e6a = 0;
     if (0 == 0)
     {
         return;
     }
     goto Label_00C0;
     Label_005C:
     num2 = 0;
     goto Label_003D;
     Label_00C0:
     this.xc0c4c459c6ccbd00 = new int[2 * this.x9fc3ee03a439f6f0];
     for (num = 0; num < this.x9fc3ee03a439f6f0; num++)
     {
         this.x32dce50116aa0f1e[num] = 1;
         this.x32dce50116aa0f1e[num + this.x9fc3ee03a439f6f0] = -1;
         this.xc0c4c459c6ccbd00[num] = num;
         this.xc0c4c459c6ccbd00[num + this.x9fc3ee03a439f6f0] = num;
     }
     this.x5cafa8d49ea71ea1 = new float[2][];
     goto Label_005C;
     Label_0115:
     this.x1f31bf6ca58166a1 = new xb730a77005d16cc1(this.x9fc3ee03a439f6f0, (int) (param.cache_size * 1048576.0));
     this.x32dce50116aa0f1e = new sbyte[2 * this.x9fc3ee03a439f6f0];
     goto Label_00C0;
 }
예제 #4
0
파일: SVMTrain.cs 프로젝트: neismit/emds
 public SVMTrain(SupportVectorMachine method, IMLDataSet dataSet)
     : base(TrainingImplementationType.OnePass)
 {
     this._x9425fdc2df7bcafc = 0;
     this._x87a7fc6a72741c2e = method;
     this.Training = dataSet;
     this._xab248fa87e95a7df = false;
     this._x77eae494203cfff5 = EncodeSVMProblem.Encode(dataSet, 0);
     this._xc7c4e9c099884228 = 1.0 / ((double) this._x87a7fc6a72741c2e.InputCount);
     this._x3c4da2980d043c95 = 1.0;
     if (0 == 0)
     {
     }
 }
        /// <summary>
        /// Encode the Encog dataset.
        /// </summary>
        ///
        /// <param name="training">The training data.</param>
        /// <param name="outputIndex"></param>
        /// <returns>The SVM problem.</returns>
        public static svm_problem Encode(IMLDataSet training,
                                         int outputIndex)
        {
            try
            {
                var result = new svm_problem {l = (int) training.Count};

                result.y = new double[result.l];
                result.x = new svm_node[result.l][];
                for (int i = 0; i < result.l; i++)
                {
                    result.x[i] = new svm_node[training.InputSize];
                }

                int elementIndex = 0;


                foreach (IMLDataPair pair  in  training)
                {
                    IMLData input = pair.Input;
                    IMLData output = pair.Ideal;
                    result.x[elementIndex] = new svm_node[input.Count];

                    for (int i = 0; i < input.Count; i++)
                    {
                        result.x[elementIndex][i] = new svm_node {index = i + 1, value_Renamed = input[i]};
                    }

                    result.y[elementIndex] = output[outputIndex];

                    elementIndex++;
                }

                return result;
            }
            catch (OutOfMemoryException )
            {
                throw new EncogError("SVM Model - Out of Memory");
            }
        }
예제 #6
0
        private static void solve_one_class(svm_problem prob, svm_parameter param, double[] alpha,
                                            Solver.SolutionInfo si)
        {
            int l = prob.l;
            var zeros = new double[l];
            var ones = new sbyte[l];
            int i;

            //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042_3"'
            var n = (int) (param.nu*prob.l); // # of alpha's at upper bound

            for (i = 0; i < n; i++)
                alpha[i] = 1;
            alpha[n] = param.nu*prob.l - n;
            for (i = n + 1; i < l; i++)
                alpha[i] = 0;

            for (i = 0; i < l; i++)
            {
                zeros[i] = 0;
                ones[i] = 1;
            }

            var s = new Solver();
            s.Solve(l, new ONE_CLASS_Q(prob, param), zeros, ones, alpha, 1.0, 1.0, param.eps, si, param.shrinking);
        }
예제 #7
0
        private static void solve_nu_svc(svm_problem prob, svm_parameter param, double[] alpha, Solver.SolutionInfo si)
        {
            int i;
            int l = prob.l;
            double nu = param.nu;

            var y = new sbyte[l];

            for (i = 0; i < l; i++)
                if (prob.y[i] > 0)
                    y[i] = (+ 1);
                else
                    y[i] = - 1;

            double sum_pos = nu*l/2;
            double sum_neg = nu*l/2;

            for (i = 0; i < l; i++)
                if (y[i] == + 1)
                {
                    alpha[i] = Math.Min(1.0, sum_pos);
                    sum_pos -= alpha[i];
                }
                else
                {
                    alpha[i] = Math.Min(1.0, sum_neg);
                    sum_neg -= alpha[i];
                }

            var zeros = new double[l];

            for (i = 0; i < l; i++)
                zeros[i] = 0;

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

            Console.Out.Write("C = " + 1/r + "\n");

            for (i = 0; i < l; i++)
                alpha[i] *= y[i]/r;

            si.rho /= r;
            si.obj /= (r*r);
            si.upper_bound_p = 1/r;
            si.upper_bound_n = 1/r;
        }
예제 #8
0
        //
        // construct and solve various formulations
        //
        private static void solve_c_svc(svm_problem prob, svm_parameter param, double[] alpha, Solver.SolutionInfo si,
                                        double Cp, double Cn)
        {
            int l = prob.l;
            var minus_ones = new double[l];
            var y = new sbyte[l];

            int i;

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

            var s = new Solver();
            s.Solve(l, new SVC_Q(prob, param, y), minus_ones, y, alpha, Cp, Cn, param.eps, si, param.shrinking);

            double sum_alpha = 0;
            for (i = 0; i < l; i++)
                sum_alpha += alpha[i];

            if (Cp == Cn)
                Console.Out.Write("nu = " + sum_alpha/(Cp*prob.l) + "\n");

            for (i = 0; i < l; i++)
                alpha[i] *= y[i];
        }
예제 #9
0
 internal SVR_Q(svm_problem prob, svm_parameter param) : base(prob.l, prob.x, param)
 {
     l = prob.l;
     //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042_3"'
     cache = new Cache(l, (int) (param.cache_size*(1 << 20)));
     sign = new sbyte[2*l];
     index = new int[2*l];
     for (int k = 0; k < l; k++)
     {
         sign[k] = 1;
         sign[k + l] = - 1;
         index[k] = k;
         index[k + l] = k;
     }
     buffer = new float[2][];
     for (int i = 0; i < 2; i++)
     {
         buffer[i] = new float[2*l];
     }
     next_buffer = 0;
 }
예제 #10
0
 public static svm_problem Encode(IMLDataSet training, int outputIndex)
 {
     svm_problem _problem3;
     try
     {
         svm_problem _problem;
         int num;
         int num2;
         svm_problem _problem2 = new svm_problem();
         goto Label_0158;
     Label_000C:
         if (1 == 0)
         {
             return _problem3;
         }
         if ((((uint) num2) + ((uint) num2)) < 0)
         {
             goto Label_018B;
         }
     Label_0031:
         if (num >= _problem.l)
         {
             num2 = 0;
             using (IEnumerator<IMLDataPair> enumerator = training.GetEnumerator())
             {
                 IMLDataPair pair;
                 IMLData input;
                 IMLData data2;
                 int num3;
                 svm_node _node;
                 goto Label_0083;
             Label_0049:
                 num3++;
             Label_004F:
                 if (num3 < input.Count)
                 {
                     goto Label_00CA;
                 }
                 _problem.y[num2] = data2[outputIndex];
                 if (((uint) outputIndex) < 0)
                 {
                     return _problem;
                 }
                 num2++;
             Label_0083:
                 if (enumerator.MoveNext())
                 {
                     goto Label_0100;
                 }
                 return _problem;
             Label_008E:
                 data2 = pair.Ideal;
                 if ((((uint) num3) + ((uint) num2)) >= 0)
                 {
                     _problem.x[num2] = new svm_node[input.Count];
                     num3 = 0;
                     goto Label_004F;
                 }
             Label_00CA:
                 _node = new svm_node();
                 _node.index = num3 + 1;
                 _node.value_Renamed = input[num3];
                 _problem.x[num2][num3] = _node;
                 goto Label_0049;
             Label_0100:
                 pair = enumerator.Current;
                 input = pair.Input;
                 goto Label_008E;
             }
         }
         _problem.x[num] = new svm_node[training.InputSize];
         num++;
         if ((((uint) outputIndex) & 0) == 0)
         {
             goto Label_000C;
         }
         return _problem3;
     Label_0158:
         _problem2.l = (int) training.Count;
         _problem = _problem2;
         _problem.y = new double[_problem.l];
         _problem.x = new svm_node[_problem.l][];
     Label_018B:
         num = 0;
         goto Label_0031;
     }
     catch (OutOfMemoryException)
     {
         throw new EncogError("SVM Model - Out of Memory");
     }
     return _problem3;
 }
예제 #11
0
        public static void svm_cross_validation(svm_problem prob, svm_parameter param, int nr_fold, double[] target)
        {
            int i;
            var perm = new int[prob.l];

            // random shuffle
            for (i = 0; i < prob.l; i++)
                perm[i] = i;
            for (i = 0; i < prob.l; i++)
            {
                //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042_3"'
                int j = i + (int) (SupportClass.Random.NextDouble()*(prob.l - i));
                do
                {
                    int _ = perm[i];
                    perm[i] = perm[j];
                    perm[j] = _;
                } while (false);
            }
            for (i = 0; i < nr_fold; i++)
            {
                int begin = i*prob.l/nr_fold;
                int end = (i + 1)*prob.l/nr_fold;
                int j, k;
                var subprob = new svm_problem();

                subprob.l = prob.l - (end - begin);
                subprob.x = new svm_node[subprob.l][];
                subprob.y = new double[subprob.l];

                k = 0;
                for (j = 0; j < begin; j++)
                {
                    subprob.x[k] = prob.x[perm[j]];
                    subprob.y[k] = prob.y[perm[j]];
                    ++k;
                }
                for (j = end; j < prob.l; j++)
                {
                    subprob.x[k] = prob.x[perm[j]];
                    subprob.y[k] = prob.y[perm[j]];
                    ++k;
                }
                svm_model submodel = svm_train(subprob, param);
                if (param.probability == 1 &&
                    (param.svm_type == svm_parameter.C_SVC || param.svm_type == svm_parameter.NU_SVC))
                {
                    var prob_estimates = new double[svm_get_nr_class(submodel)];
                    for (j = begin; j < end; j++)
                        target[perm[j]] = svm_predict_probability(submodel, prob.x[perm[j]], prob_estimates);
                }
                else
                    for (j = begin; j < end; j++)
                        target[perm[j]] = svm_predict(submodel, prob.x[perm[j]]);
            }
        }
예제 #12
0
        internal static decision_function svm_train_one(svm_problem prob, svm_parameter param, double Cp, double Cn)
        {
            var alpha = new double[prob.l];
            var si = new Solver.SolutionInfo();
            switch (param.svm_type)
            {
                case svm_parameter.C_SVC:
                    solve_c_svc(prob, param, alpha, si, Cp, Cn);
                    break;

                case svm_parameter.NU_SVC:
                    solve_nu_svc(prob, param, alpha, si);
                    break;

                case svm_parameter.ONE_CLASS:
                    solve_one_class(prob, param, alpha, si);
                    break;

                case svm_parameter.EPSILON_SVR:
                    solve_epsilon_svr(prob, param, alpha, si);
                    break;

                case svm_parameter.NU_SVR:
                    solve_nu_svr(prob, param, alpha, si);
                    break;
            }

            Console.Out.Write("obj = " + si.obj + ", rho = " + si.rho + "\n");

            // output SVs

            int nSV = 0;
            int nBSV = 0;
            for (int i = 0; i < prob.l; i++)
            {
                if (Math.Abs(alpha[i]) > 0)
                {
                    ++nSV;
                    if (prob.y[i] > 0)
                    {
                        if (Math.Abs(alpha[i]) >= si.upper_bound_p)
                            ++nBSV;
                    }
                    else
                    {
                        if (Math.Abs(alpha[i]) >= si.upper_bound_n)
                            ++nBSV;
                    }
                }
            }

            Console.Out.Write("nSV = " + nSV + ", nBSV = " + nBSV + "\n");

            var f = new decision_function();
            f.alpha = alpha;
            f.rho = si.rho;
            return f;
        }
예제 #13
0
        /// <summary>
        /// Evaluate the error for the specified model.
        /// </summary>
        ///
        /// <param name="param">The params for the SVN.</param>
        /// <param name="prob">The problem to evaluate.</param>
        /// <param name="target">The output values from the SVN.</param>
        /// <returns>The calculated error.</returns>
        private static double Evaluate(svm_parameter param, svm_problem prob,
                                double[] target)
        {
            int totalCorrect = 0;

            var error = new ErrorCalculation();

            if ((param.svm_type == svm_parameter.EPSILON_SVR)
                || (param.svm_type == svm_parameter.NU_SVR))
            {
                for (int i = 0; i < prob.l; i++)
                {
                    double ideal = prob.y[i];
                    double actual = target[i];
                    error.UpdateError(actual, ideal);
                }
                return error.Calculate();
            }
            for (int i = 0; i < prob.l; i++)
            {
                if (target[i] == prob.y[i])
                {
                    ++totalCorrect;
                }
            }

            return Format.HundredPercent*totalCorrect/prob.l;
        }
예제 #14
0
        /// <summary>
        /// Construct a trainer for an SVM network.
        /// </summary>
        ///
        /// <param name="method">The network to train.</param>
        /// <param name="dataSet">The training data for this network.</param>
        public SVMTrain(SupportVectorMachine method, IMLDataSet dataSet) : base(TrainingImplementationType.OnePass)
        {
            _fold = 0;
            _network = method;
            Training = dataSet;
            _trainingDone = false;

            _problem = EncodeSVMProblem.Encode(dataSet, 0);
            _gamma = 1.0d/_network.InputCount;
            _c = 1.0d;
        }
예제 #15
0
파일: SVMTrain.cs 프로젝트: neismit/emds
 private static double x308cb2f3483de2a6(svm_parameter x0d173b5435b4d6ad, svm_problem xdee3898b83df48b4, double[] x11d58b056c032b03)
 {
     ErrorCalculation calculation;
     int num2;
     double num3;
     int num5;
     int num = 0;
     if (0 == 0)
     {
         if ((((uint) num3) & 0) != 0)
         {
             goto Label_0134;
         }
         goto Label_0108;
     }
     goto Label_008C;
     Label_0055:
     if (num2 >= xdee3898b83df48b4.l)
     {
         return calculation.Calculate();
     }
     Label_008C:
     num3 = xdee3898b83df48b4.y[num2];
     double actual = x11d58b056c032b03[num2];
     if (((uint) num3) < 0)
     {
         goto Label_0108;
     }
     calculation.UpdateError(actual, num3);
     num2++;
     if ((((uint) num3) - ((uint) num3)) >= 0)
     {
         goto Label_0055;
     }
     Label_00D4:
     if ((((uint) num5) & 0) == 0)
     {
         while (num5 < xdee3898b83df48b4.l)
         {
             while (x11d58b056c032b03[num5] == xdee3898b83df48b4.y[num5])
             {
                 num++;
                 if ((((uint) actual) + ((uint) actual)) <= uint.MaxValue)
                 {
                     break;
                 }
             }
             num5++;
         }
     }
     goto Label_0134;
     Label_0108:
     calculation = new ErrorCalculation();
     if (((x0d173b5435b4d6ad.svm_type != 3) && ((((uint) num5) | 3) != 0)) && (x0d173b5435b4d6ad.svm_type != 4))
     {
         num5 = 0;
         if ((((uint) num3) + ((uint) actual)) <= uint.MaxValue)
         {
             goto Label_00D4;
         }
         goto Label_008C;
     }
     num2 = 0;
     goto Label_0055;
     Label_0134:
     return ((100.0 * num) / ((double) xdee3898b83df48b4.l));
 }
예제 #16
0
        private static void solve_epsilon_svr(svm_problem prob, svm_parameter param, double[] alpha,
                                              Solver.SolutionInfo si)
        {
            int l = prob.l;
            var alpha2 = new double[2*l];
            var linear_term = new double[2*l];
            var y = new sbyte[2*l];
            int i;

            for (i = 0; i < l; i++)
            {
                alpha2[i] = 0;
                linear_term[i] = param.p - prob.y[i];
                y[i] = 1;

                alpha2[i + l] = 0;
                linear_term[i + l] = param.p + prob.y[i];
                y[i + l] = - 1;
            }

            var s = new Solver();
            s.Solve(2*l, new SVR_Q(prob, param), linear_term, y, alpha2, param.C, param.C, param.eps, si,
                    param.shrinking);

            double sum_alpha = 0;
            for (i = 0; i < l; i++)
            {
                alpha[i] = alpha2[i] - alpha2[i + l];
                sum_alpha += Math.Abs(alpha[i]);
            }
            Console.Out.Write("nu = " + sum_alpha/(param.C*l) + "\n");
        }
예제 #17
0
        // Return parameter of a Laplace distribution 
        private static double svm_svr_probability(svm_problem prob, svm_parameter param)
        {
            int i;
            int nr_fold = 5;
            var ymv = new double[prob.l];
            double mae = 0;

            var newparam = (svm_parameter) param.Clone();
            newparam.probability = 0;
            svm_cross_validation(prob, newparam, nr_fold, ymv);
            for (i = 0; i < prob.l; i++)
            {
                ymv[i] = prob.y[i] - ymv[i];
                mae += Math.Abs(ymv[i]);
            }
            mae /= prob.l;
            double std = Math.Sqrt(2*mae*mae);
            int count = 0;
            mae = 0;
            for (i = 0; i < prob.l; i++)
                if (Math.Abs(ymv[i]) > 5*std)
                    count = count + 1;
                else
                    mae += Math.Abs(ymv[i]);
            mae /= (prob.l - count);
            Console.Error.Write(
                "Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=" +
                mae + "\n");
            return mae;
        }
예제 #18
0
        private static void solve_nu_svr(svm_problem prob, svm_parameter param, double[] alpha, Solver.SolutionInfo si)
        {
            int l = prob.l;
            double C = param.C;
            var alpha2 = new double[2*l];
            var linear_term = new double[2*l];
            var y = new sbyte[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];

                linear_term[i] = - prob.y[i];
                y[i] = 1;

                linear_term[i + l] = prob.y[i];
                y[i + l] = - 1;
            }

            var s = new Solver_NU();
            s.Solve(2*l, new SVR_Q(prob, param), linear_term, y, alpha2, C, C, param.eps, si, param.shrinking);

            Console.Out.Write("epsilon = " + (- si.r) + "\n");

            for (i = 0; i < l; i++)
                alpha[i] = alpha2[i] - alpha2[i + l];
        }
예제 #19
0
        /// <summary>
        /// Evaluate the error for the specified model.
        /// </summary>
        /// <param name="param">The params for the SVN.</param>
        /// <param name="prob">The problem to evaluate.</param>
        /// <param name="target">The output values from the SVN.</param>
        /// <returns>The calculated error.</returns>
        private double Evaluate(svm_parameter param, svm_problem prob,
                double[] target)
        {
            int total_correct = 0;

            ErrorCalculation error = new ErrorCalculation();

            if (param.svm_type == svm_parameter.EPSILON_SVR
                    || param.svm_type == svm_parameter.NU_SVR)
            {
                for (int i = 0; i < prob.l; i++)
                {
                    double ideal = prob.y[i];
                    double actual = target[i];
                    error.UpdateError(actual, ideal);
                }
                return error.Calculate();
            }
            else
            {
                for (int i = 0; i < prob.l; i++)
                    if (target[i] == prob.y[i])
                        ++total_correct;

                return 100.0 * total_correct / prob.l;
            }
        }
예제 #20
0
        // Cross-validation decision values for probability estimates
        private static void svm_binary_svc_probability(svm_problem prob, svm_parameter param, double Cp, double Cn,
                                                       double[] probAB)
        {
            int i;
            int nr_fold = 5;
            var perm = new int[prob.l];
            var dec_values = new double[prob.l];

            // random shuffle
            for (i = 0; i < prob.l; i++)
                perm[i] = i;
            for (i = 0; i < prob.l; i++)
            {
                //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042_3"'
                int j = i + (int) (SupportClass.Random.NextDouble()*(prob.l - i));
                do
                {
                    int _ = perm[i];
                    perm[i] = perm[j];
                    perm[j] = _;
                } while (false);
            }
            for (i = 0; i < nr_fold; i++)
            {
                int begin = i*prob.l/nr_fold;
                int end = (i + 1)*prob.l/nr_fold;
                int j, k;
                var subprob = new svm_problem();

                subprob.l = prob.l - (end - begin);
                subprob.x = new svm_node[subprob.l][];
                subprob.y = new double[subprob.l];

                k = 0;
                for (j = 0; j < begin; j++)
                {
                    subprob.x[k] = prob.x[perm[j]];
                    subprob.y[k] = prob.y[perm[j]];
                    ++k;
                }
                for (j = end; j < prob.l; j++)
                {
                    subprob.x[k] = prob.x[perm[j]];
                    subprob.y[k] = prob.y[perm[j]];
                    ++k;
                }
                int p_count = 0, n_count = 0;
                for (j = 0; j < k; j++)
                    if (subprob.y[j] > 0)
                        p_count++;
                    else
                        n_count++;

                if (p_count == 0 && n_count == 0)
                    for (j = begin; j < end; j++)
                        dec_values[perm[j]] = 0;
                else if (p_count > 0 && n_count == 0)
                    for (j = begin; j < end; j++)
                        dec_values[perm[j]] = 1;
                else if (p_count == 0 && n_count > 0)
                    for (j = begin; j < end; j++)
                        dec_values[perm[j]] = - 1;
                else
                {
                    var subparam = (svm_parameter) param.Clone();
                    subparam.probability = 0;
                    subparam.C = 1.0;
                    subparam.nr_weight = 2;
                    subparam.weight_label = new int[2];
                    subparam.weight = new double[2];
                    subparam.weight_label[0] = + 1;
                    subparam.weight_label[1] = - 1;
                    subparam.weight[0] = Cp;
                    subparam.weight[1] = Cn;
                    svm_model submodel = svm_train(subprob, subparam);
                    for (j = begin; j < end; j++)
                    {
                        var dec_value = new double[1];
                        svm_predict_values(submodel, prob.x[perm[j]], dec_value);
                        dec_values[perm[j]] = dec_value[0];
                        // ensure +1 -1 order; reason not using CV subroutine
                        dec_values[perm[j]] *= submodel.label[0];
                    }
                }
            }
            sigmoid_train(prob.l, dec_values, prob.y, probAB);
        }
예제 #21
0
 internal SVC_Q(svm_problem prob, svm_parameter param, sbyte[] y_) : base(prob.l, prob.x, param)
 {
     y = new sbyte[y_.Length];
     y_.CopyTo(y, 0);
     //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042_3"'
     cache = new Cache(prob.l, (int) (param.cache_size*(1 << 20)));
 }
예제 #22
0
        //
        // Interface functions
        //
        public static svm_model svm_train(svm_problem prob, svm_parameter param)
        {
            var model = new svm_model();
            model.param = param;

            if (param.svm_type == svm_parameter.ONE_CLASS || param.svm_type == svm_parameter.EPSILON_SVR ||
                param.svm_type == svm_parameter.NU_SVR)
            {
                // regression or one-class-svm
                model.nr_class = 2;
                model.label = null;
                model.nSV = null;
                model.probA = null;
                model.probB = null;
                model.sv_coef = new double[1][];

                if (param.probability == 1 &&
                    (param.svm_type == svm_parameter.EPSILON_SVR || param.svm_type == svm_parameter.NU_SVR))
                {
                    model.probA = new double[1];
                    model.probA[0] = svm_svr_probability(prob, param);
                }

                decision_function f = svm_train_one(prob, param, 0, 0);
                model.rho = new double[1];
                model.rho[0] = f.rho;

                int nSV = 0;
                int i;
                for (i = 0; i < prob.l; i++)
                    if (Math.Abs(f.alpha[i]) > 0)
                        ++nSV;
                model.l = nSV;
                model.SV = new svm_node[nSV][];
                model.sv_coef[0] = new double[nSV];
                int j = 0;
                for (i = 0; i < prob.l; i++)
                    if (Math.Abs(f.alpha[i]) > 0)
                    {
                        model.SV[j] = prob.x[i];
                        model.sv_coef[0][j] = f.alpha[i];
                        ++j;
                    }
            }
            else
            {
                // classification
                // find out the number of classes
                int l = prob.l;
                int max_nr_class = 16;
                int nr_class = 0;
                var label = new int[max_nr_class];
                var count = new int[max_nr_class];
                var index = new int[l];

                int i;
                for (i = 0; i < l; i++)
                {
                    //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042_3"'
                    var this_label = (int) prob.y[i];
                    int j;
                    for (j = 0; j < nr_class; j++)
                        if (this_label == label[j])
                        {
                            ++count[j];
                            break;
                        }
                    index[i] = j;
                    if (j == nr_class)
                    {
                        if (nr_class == max_nr_class)
                        {
                            max_nr_class *= 2;
                            var new_data = new int[max_nr_class];
                            Array.Copy(label, 0, new_data, 0, label.Length);
                            label = new_data;

                            new_data = new int[max_nr_class];
                            Array.Copy(count, 0, new_data, 0, count.Length);
                            count = new_data;
                        }
                        label[nr_class] = this_label;
                        count[nr_class] = 1;
                        ++nr_class;
                    }
                }

                // group training data of the same class

                var start = new int[nr_class];
                start[0] = 0;
                for (i = 1; i < nr_class; i++)
                    start[i] = start[i - 1] + count[i - 1];

                var x = new svm_node[l][];

                for (i = 0; i < l; i++)
                {
                    x[start[index[i]]] = prob.x[i];
                    ++start[index[i]];
                }

                start[0] = 0;
                for (i = 1; i < nr_class; i++)
                    start[i] = start[i - 1] + count[i - 1];

                // calculate weighted C

                var weighted_C = new double[nr_class];
                for (i = 0; i < nr_class; i++)
                    weighted_C[i] = param.C;
                for (i = 0; i < param.nr_weight; i++)
                {
                    int j;
                    for (j = 0; j < nr_class; j++)
                        if (param.weight_label[i] == label[j])
                            break;
                    if (j == nr_class)
                        Console.Error.Write("warning: class label " + param.weight_label[i] +
                                            " specified in weight is not found\n");
                    else
                        weighted_C[j] *= param.weight[i];
                }

                // train k*(k-1)/2 models

                var nonzero = new bool[l];
                for (i = 0; i < l; i++)
                    nonzero[i] = false;
                var f = new decision_function[nr_class*(nr_class - 1)/2];

                double[] probA = null, probB = null;
                if (param.probability == 1)
                {
                    probA = new double[nr_class*(nr_class - 1)/2];
                    probB = new double[nr_class*(nr_class - 1)/2];
                }

                int p = 0;
                for (i = 0; i < nr_class; i++)
                    for (int j = i + 1; j < nr_class; j++)
                    {
                        var sub_prob = new svm_problem();
                        int si = start[i], sj = start[j];
                        int ci = count[i], cj = count[j];
                        sub_prob.l = ci + cj;
                        sub_prob.x = new svm_node[sub_prob.l][];
                        sub_prob.y = new double[sub_prob.l];
                        int k;
                        for (k = 0; k < ci; k++)
                        {
                            sub_prob.x[k] = x[si + k];
                            sub_prob.y[k] = + 1;
                        }
                        for (k = 0; k < cj; k++)
                        {
                            sub_prob.x[ci + k] = x[sj + k];
                            sub_prob.y[ci + k] = - 1;
                        }

                        if (param.probability == 1)
                        {
                            var probAB = new double[2];
                            svm_binary_svc_probability(sub_prob, param, weighted_C[i], weighted_C[j], probAB);
                            probA[p] = probAB[0];
                            probB[p] = probAB[1];
                        }

                        f[p] = svm_train_one(sub_prob, param, weighted_C[i], weighted_C[j]);
                        for (k = 0; k < ci; k++)
                            if (!nonzero[si + k] && Math.Abs(f[p].alpha[k]) > 0)
                                nonzero[si + k] = true;
                        for (k = 0; k < cj; k++)
                            if (!nonzero[sj + k] && Math.Abs(f[p].alpha[ci + k]) > 0)
                                nonzero[sj + k] = true;
                        ++p;
                    }

                // build output

                model.nr_class = nr_class;

                model.label = new int[nr_class];
                for (i = 0; i < nr_class; i++)
                    model.label[i] = label[i];

                model.rho = new double[nr_class*(nr_class - 1)/2];
                for (i = 0; i < nr_class*(nr_class - 1)/2; i++)
                    model.rho[i] = f[i].rho;

                if (param.probability == 1)
                {
                    model.probA = new double[nr_class*(nr_class - 1)/2];
                    model.probB = new double[nr_class*(nr_class - 1)/2];
                    for (i = 0; i < nr_class*(nr_class - 1)/2; i++)
                    {
                        model.probA[i] = probA[i];
                        model.probB[i] = probB[i];
                    }
                }
                else
                {
                    model.probA = null;
                    model.probB = null;
                }

                int nnz = 0;
                var nz_count = new int[nr_class];
                model.nSV = new int[nr_class];
                for (i = 0; i < nr_class; i++)
                {
                    int nSV = 0;
                    for (int j = 0; j < count[i]; j++)
                        if (nonzero[start[i] + j])
                        {
                            ++nSV;
                            ++nnz;
                        }
                    model.nSV[i] = nSV;
                    nz_count[i] = nSV;
                }

                Console.Out.Write("Total nSV = " + nnz + "\n");

                model.l = nnz;
                model.SV = new svm_node[nnz][];
                p = 0;
                for (i = 0; i < l; i++)
                    if (nonzero[i])
                        model.SV[p++] = x[i];

                var nz_start = new int[nr_class];
                nz_start[0] = 0;
                for (i = 1; i < nr_class; i++)
                    nz_start[i] = nz_start[i - 1] + nz_count[i - 1];

                model.sv_coef = new double[nr_class - 1][];
                for (i = 0; i < nr_class - 1; i++)
                    model.sv_coef[i] = new double[nnz];

                p = 0;
                for (i = 0; i < nr_class; i++)
                    for (int j = i + 1; j < nr_class; j++)
                    {
                        // classifier (i,j): coefficients with
                        // i are in sv_coef[j-1][nz_start[i]...],
                        // j are in sv_coef[i][nz_start[j]...]

                        int si = start[i];
                        int sj = start[j];
                        int ci = count[i];
                        int cj = count[j];

                        int q = nz_start[i];
                        int k;
                        for (k = 0; k < ci; k++)
                            if (nonzero[si + k])
                                model.sv_coef[j - 1][q++] = f[p].alpha[k];
                        q = nz_start[j];
                        for (k = 0; k < cj; k++)
                            if (nonzero[sj + k])
                                model.sv_coef[i][q++] = f[p].alpha[ci + k];
                        ++p;
                    }
            }
            return model;
        }
예제 #23
0
 internal ONE_CLASS_Q(svm_problem prob, svm_parameter param) : base(prob.l, prob.x, param)
 {
     //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042_3"'
     cache = new Cache(prob.l, (int) (param.cache_size*(1 << 20)));
 }
예제 #24
0
        public static String svm_check_parameter(svm_problem prob, svm_parameter param)
        {
            // svm_type

            int svm_type = param.svm_type;
            if (svm_type != svm_parameter.C_SVC && svm_type != svm_parameter.NU_SVC &&
                svm_type != svm_parameter.ONE_CLASS && svm_type != svm_parameter.EPSILON_SVR &&
                svm_type != svm_parameter.NU_SVR)
                return "unknown svm type";

            // kernel_type

            int kernel_type = param.kernel_type;
            if (kernel_type != svm_parameter.LINEAR && kernel_type != svm_parameter.POLY &&
                kernel_type != svm_parameter.RBF && kernel_type != svm_parameter.SIGMOID)
                return "unknown kernel type";

            // cache_size,eps,C,nu,p,shrinking

            if (param.cache_size <= 0)
                return "cache_size <= 0";

            if (param.eps <= 0)
                return "eps <= 0";

            if (svm_type == svm_parameter.C_SVC || svm_type == svm_parameter.EPSILON_SVR ||
                svm_type == svm_parameter.NU_SVR)
                if (param.C <= 0)
                    return "C <= 0";

            if (svm_type == svm_parameter.NU_SVC || svm_type == svm_parameter.ONE_CLASS ||
                svm_type == svm_parameter.NU_SVR)
                if (param.nu < 0 || param.nu > 1)
                    return "nu < 0 or nu > 1";

            if (svm_type == svm_parameter.EPSILON_SVR)
                if (param.p < 0)
                    return "p < 0";

            if (param.shrinking != 0 && param.shrinking != 1)
                return "shrinking != 0 and shrinking != 1";

            if (param.probability != 0 && param.probability != 1)
                return "probability != 0 and probability != 1";

            if (param.probability == 1 && svm_type == svm_parameter.ONE_CLASS)
                return "one-class SVM probability output not supported yet";

            // check whether nu-svc is feasible

            if (svm_type == svm_parameter.NU_SVC)
            {
                int l = prob.l;
                int max_nr_class = 16;
                int nr_class = 0;
                var label = new int[max_nr_class];
                var count = new int[max_nr_class];

                int i;
                for (i = 0; i < l; i++)
                {
                    //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042_3"'
                    var this_label = (int) prob.y[i];
                    int j;
                    for (j = 0; j < nr_class; j++)
                        if (this_label == label[j])
                        {
                            ++count[j];
                            break;
                        }

                    if (j == nr_class)
                    {
                        if (nr_class == max_nr_class)
                        {
                            max_nr_class *= 2;
                            var new_data = new int[max_nr_class];
                            Array.Copy(label, 0, new_data, 0, label.Length);
                            label = new_data;

                            new_data = new int[max_nr_class];
                            Array.Copy(count, 0, new_data, 0, count.Length);
                            count = new_data;
                        }
                        label[nr_class] = this_label;
                        count[nr_class] = 1;
                        ++nr_class;
                    }
                }

                for (i = 0; i < nr_class; i++)
                {
                    int n1 = count[i];
                    for (int j = i + 1; j < nr_class; j++)
                    {
                        int n2 = count[j];
                        if (param.nu*(n1 + n2)/2 > Math.Min(n1, n2))
                            return "specified nu is infeasible";
                    }
                }
            }

            return null;
        }
예제 #25
0
 internal x4607fc510d7665cf(svm_problem prob, svm_parameter param)
     : base(prob.l, prob.x, param)
 {
     this.x1f31bf6ca58166a1 = new xb730a77005d16cc1(prob.l, (int) (param.cache_size * 1048576.0));
 }