Exemplo n.º 1
0
        public static svm_problem ReadProblem(string input_file_name)
        {
            var vy = new List<double>();
            var vx = new List<svm_node[]>();
            using (var sr = new StreamReader(input_file_name))
            {
                while (true)
                {
                    string line = sr.ReadLine();
                    if (line == null) break;

                    string[] st = line.Split(" \t\n\r\f".ToCharArray()).Where(c => c != String.Empty).ToArray();

                    vy.Add(st[0].ToDouble());

                    int m = (st.Count() - 1);
                    var x = new List<svm_node>();
                    for (int i = 0; i < m; i++)
                    {
                        string[] values = st[i + 1].Trim().Split(':');
                        double value = values[1].ToDouble();
                        x.Add(new svm_node
                        {
                            index = values[0].ToInteger(),
                            value = value,
                        });
                    }
                    vx.Add(x.ToArray());
                }
            }
            var prob = new svm_problem {l = vy.Count, x = vx.ToArray(), y = vy.ToArray()};

            return prob;
        }
Exemplo n.º 2
0
        public void Should_be_able_to_scale_problems()
        {
            var prob = new svm_problem() {
                l = 2,
                x = new svm_node[][]
                {
                   new svm_node[]
                   {
                        new svm_node(){ index= 1, value=1},
                        new svm_node(){ index= 2, value=2}
                   },
                   new svm_node[]
                   {
                        new svm_node(){ index= 1, value=3},
                        new svm_node(){ index= 2, value=4}
                   }
                },
                y = new double[] { 0, 0 }
            };

            prob = prob.Scale(0, 1);
            Assert.IsTrue(prob.x[0].Single(x => x.index == 1).value == 0);
            Assert.IsTrue(prob.x[1].Single(x => x.index == 1).value == 1);

            Assert.IsTrue(prob.x[0].Single(x => x.index == 2).value == 0);
            Assert.IsTrue(prob.x[1].Single(x => x.index == 2).value == 1);
        }
Exemplo n.º 3
0
 public C_SVC_Tests()
 {
     var current_path = Environment.CurrentDirectory;
     var pos = current_path.IndexOf("libsvm.net");
     base_path = current_path.Substring(0, pos + 10);
     string full_path = System.IO.Path.Combine(base_path, XOR_DATASET);
     xor_problem = ProblemHelper.ReadProblem(full_path);
 }
Exemplo n.º 4
0
 public void TestInitialize()
 {
     var path = Environment.CurrentDirectory;
     var pos = path.IndexOf("libsvm.net");
     var basePath = path.Substring(0, pos + 10);
     training_prob = ProblemHelper.ReadAndScaleProblem(System.IO.Path.Combine(basePath, TRAINING_FILE));
     test_prob = ProblemHelper.ReadAndScaleProblem(System.IO.Path.Combine(basePath,TEST_FILE));
     full_prob = ProblemHelper.ReadAndScaleProblem(System.IO.Path.Combine(basePath,FULL_FILE));
 }
        public svm_problem CreateProblem()
        {
            svm_problem problem = new svm_problem();
            problem.l = labels.Length;
            problem.y = labels;
            problem.x = this.problemSpace.Select(problemVector => MatrixUtil.DoubleToSvmNode(problemVector)).ToArray();

            return problem;
        }
Exemplo n.º 6
0
        public void TestInitialize()
        {
            var path = Environment.CurrentDirectory;
            var pos = path.IndexOf("libsvm.net");
            var basePath = path.Substring(0, pos + 10);
            string fullPath = System.IO.Path.Combine(basePath, LEU_TEST_FILE);

            // get data from file
            // Note that you should always scale your data
            _prob = ProblemHelper.ReadAndScaleProblem(fullPath);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Default SVM
        /// </summary>
        /// <remarks>The class store svm parameters and create the model.
        /// This way, you can use it to predict</remarks>
        public SVM(svm_problem prob, svm_parameter param)
        {
            var error = svm.svm_check_parameter(prob, param);
            if (error != null)
            {
                throw new Exception(error);
            }

            this.prob = prob;
            this.param = param;

            this.Train();
        }
Exemplo n.º 8
0
        public static svm_problem ScaleProblem(svm_problem prob, double lower = -1.0, double upper = 1.0)
        {
            var index_max = prob.x.Max(X => X.Max(e=>e.index));
            var feature_max = new double[(index_max + 1)];
            var feature_min = new double[(index_max + 1)];
            int n = prob.l;

            for (int i = 0; i <= index_max; i++)
            {
                feature_max[i] = -Double.MaxValue;
                feature_min[i] = Double.MaxValue;
            }

            for (int i = 0; i < n; i++)
            {
                var m = prob.x[i].Count();
                for (int j = 0; j< m; j++)
                {
                    var index = prob.x[i][j].index;
                    feature_max[index - 1] = Math.Max(feature_max[index - 1], prob.x[i][j].value);
                    feature_min[index - 1] = Math.Min(feature_min[index - 1], prob.x[i][j].value);
                }
            }
            
            var scaledProb = new svm_problem();
            scaledProb.l = n;
            scaledProb.y = prob.y.ToArray();
            scaledProb.x = new svm_node[n][];
            for (int i = 0; i < n; i++)
            {
                var m = prob.x[i].Count();
                scaledProb.x[i] = new svm_node[m];
                for (int j = 0; j < m; j++)
                {
                    var index = prob.x[i][j].index;
                    var value = prob.x[i][j].value;
                    var max = feature_max[index - 1];
                    var min = feature_min[index - 1];

                    scaledProb.x[i][j] = new svm_node() { index = index };

                    if (min == max)
                        scaledProb.x[i][j].value = 0;
                    else
                        scaledProb.x[i][j].value = lower + (upper - lower) * (value - min) / (max - min);
                }
            }
            return scaledProb;
        }
Exemplo n.º 9
0
        public static svm_problem ScaleProblem(svm_problem prob, double lower = -1.0, double upper = 1.0)
        {
            int indexMax = prob.x.Max(X => X.Max(e => e.index));
            var featureMax = new double[(indexMax + 1)];
            var featureMin = new double[(indexMax + 1)];
            int n = prob.l;

            for (int i = 0; i <= indexMax; i++)
            {
                featureMax[i] = -Double.MaxValue;
                featureMin[i] = Double.MaxValue;
            }

            for (int i = 0; i < n; i++)
            {
                int m = prob.x[i].Count();
                for (int j = 0; j < m; j++)
                {
                    int index = prob.x[i][j].index;
                    featureMax[index - 1] = Math.Max(featureMax[index - 1], prob.x[i][j].value);
                    featureMin[index - 1] = Math.Min(featureMin[index - 1], prob.x[i][j].value);
                }
            }

            var scaledProb = new svm_problem {l = n, y = prob.y.ToArray(), x = new svm_node[n][]};

            for (int i = 0; i < n; i++)
            {
                int m = prob.x[i].Count();
                scaledProb.x[i] = new svm_node[m];
                for (int j = 0; j < m; j++)
                {
                    int index = prob.x[i][j].index;
                    double value = prob.x[i][j].value;
                    double max = featureMax[index - 1];
                    double min = featureMin[index - 1];

                    scaledProb.x[i][j] = new svm_node {index = index};

                    if (Math.Abs(min - max) < double.Epsilon)
                        scaledProb.x[i][j].value = 0;
                    else
                        scaledProb.x[i][j].value = lower + (upper - lower)*(value - min)/(max - min);
                }
            }
            return scaledProb;
        }
Exemplo n.º 10
0
        public static svm_problem ReadProblem(string input_file_name)
        {
            
            var vy = new List<double>();
            var vx = new List<svm_node[]>();
            using (StreamReader sr = new StreamReader(input_file_name))
            {
                while (true)
                {
                    var line = sr.ReadLine();
                    if (line == null) break;

                    var st = line.Split(" \t\n\r\f".ToCharArray()).Where(c=>c!=String.Empty).ToArray();

                    vy.Add(atof(st[0]));

                    int m = (st.Count()-1);
                    List<svm_node> x = new List<svm_node>();
                    for (int i = 0; i < m; i++)
                    {
                        var values = st[i + 1].Trim().Split(':');
                        var value = atof(values[1]);
                        x.Add( new svm_node()
                        {
                            index = atoi(values[0]),
                            value = value,
                        });
                        
                    }
                    vx.Add(x.ToArray());
                }

            }
            var prob = new svm_problem();
            prob.l = vy.Count;
            prob.x = vx.ToArray();
            prob.y = vy.ToArray();

            return prob;
        }
Exemplo n.º 11
0
 /// <summary>
 /// Default SVM
 /// </summary>
 /// <remarks>The class store svm parameters and create the model. 
 /// This way, you can use it to predict</remarks>
 public SVM(svm_problem prob, int svm_type, int kernel_type, int degree, 
     double C, double gamma, double coef0, double nu, double cache_size,
     double eps, double p, int shrinking, int probability, int nr_weight, 
     int[] weight_label, double[] weight)
     :this(prob, new svm_parameter()
     {
         svm_type = svm_type,
         kernel_type = kernel_type,
         degree = degree,
         C = C,
         gamma = gamma,
         coef0 = coef0,
         nu = nu,
         cache_size = cache_size,
         eps = eps,
         p = p,
         shrinking = shrinking,
         probability = probability,
         nr_weight = nr_weight,
         weight_label = weight_label,
         weight = weight,
     })
 { }
Exemplo n.º 12
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;
            double[] ymv = new double[prob.l];
            double mae = 0;

            svm_parameter 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 += System.Math.Abs(ymv[i]);
            }
            mae /= prob.l;
            double std = System.Math.Sqrt(2 * mae * mae);
            int count = 0;
            mae = 0;
            for (i = 0; i < prob.l; i++)
                if (System.Math.Abs(ymv[i]) > 5 * std)
                    count = count + 1;
                else
                    mae += System.Math.Abs(ymv[i]);
            mae /= (prob.l - count);
            System.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;
        }
Exemplo n.º 13
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;
            int[] perm = new int[prob.l];
            double[] 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;
                svm_problem 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
                {
                    svm_parameter 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++)
                    {
                        double[] 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);
        }
Exemplo n.º 14
0
 /// <summary>
 /// Create and train the Epsilon_SV
 /// </summary>
 /// <param name="prob">Training Data Set</param>
 /// <param name="kernel">Selected Kernel</param>
 /// <param name="probability">Specify if probability are needed</param>
 /// <param name="cache_size">Indicates the maximum memory that can use the program</param>
 public Epsilon_SVR(svm_problem prob, Kernel kernel, double C, double epsilon, bool probability = true, double cache_size = 100)
     : base(SvmType.EPSILON_SVR, prob, kernel, C, epsilon, probability, cache_size)
 {
 }
Exemplo n.º 15
0
 public static void WriteProblem(string output_file_name, svm_problem problem)
 {
     using (StreamWriter sw = new StreamWriter(output_file_name))
     {
         for (int i =0 ; i< problem.l;i++)
         {
             var sb = new StringBuilder();
             sb.AppendFormat("{0} ", problem.y[i] );
             for (int j = 0; j < problem.x[i].Count(); j++)
             {
                 var node = problem.x[i][j];
                 sb.AppendFormat("{0}:{1} ", node.index, node.value);
             }
             sw.WriteLine(sb.ToString().Trim());
         }
         sw.Close();
     }
 }
Exemplo n.º 16
0
        internal static decision_function svm_train_one(svm_problem prob, svm_parameter param, double Cp, double Cn)
        {
            double[] alpha = new double[prob.l];
            Solver.SolutionInfo 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;
                }

            //Debug.WriteLine("obj = " + si.obj + ", rho = " + si.rho + "\n");

            // output SVs

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

            //Debug.WriteLine("nSV = " + nSV + ", nBSV = " + nBSV + "\n");

            decision_function f = new decision_function();
            f.alpha = alpha;
            f.rho = si.rho;
            return f;
        }
Exemplo n.º 17
0
        public static System.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;
                int[] label = new int[max_nr_class];
                int[] 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"'
                    int 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;
                            int[] 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 > System.Math.Min(n1, n2))
                            return "specified nu is infeasible";
                    }
                }
            }

            return null;
        }
Exemplo n.º 18
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)));
 }
Exemplo n.º 19
0
 /// <summary>
 /// Create and train the Epsilon_SV
 /// </summary>
 /// <param name="prob">Training Data Set</param>
 /// <param name="kernel">Selected Kernel</param>
 /// <param name="probability">Specify if probability are needed</param>
 /// <param name="cache_size">Indicates the maximum memory that can use the program</param>
 public Epsilon_SVR(svm_problem prob, Kernel kernel, double C, double epsilon, bool probability = true, double cache_size = 100)
     : base(SvmType.EPSILON_SVR, prob, kernel, C, epsilon, probability, cache_size)
 {
 }
Exemplo n.º 20
0
 public static svm_problem Scale(this svm_problem prob, double lower = -1.0, double upper = 1.0)
 {
     return(ScaleProblem(prob, lower, upper));
 }
Exemplo n.º 21
0
        /// <summary>
        ///   Runs polynomial-kernel SVM C-classifier on a specified digit vs all other digits 
        /// </summary>
        /// <param name="digit"></param>
        /// <returns>
        ///   Ein, # of support vectors
        /// </returns>
        static Tuple<double, int> Run1VsAll(int digit)
        {
            var prob = new svm_problem()
              {
            x = trainingData.Select(v =>
              new svm_node[] {
              new svm_node() { index = 0, value = v[1] },
              new svm_node() { index = 1, value = v[2] } }).ToArray(),
            y = trainingData.Select(v => (v[0] == digit ? 1.0 : -1.0)).ToArray(),
            l = trainingData.Length
              };

              var model = svm.svm_train(prob, new svm_parameter()
              {
            svm_type = (int)SvmType.C_SVC,
            kernel_type = (int)KernelType.POLY,
            C = 0.01,
            degree = 2,
            coef0 = 1,
            gamma = 1,
            eps = 0.001
              });

              return Tuple.Create((prob.x.Zip(prob.y, (v, u) => new { x = v, y = u }).Count(v =>
            Math.Sign(svm.svm_predict(model, v.x)) != Math.Sign(v.y)) + 0.0) / prob.x.Length,
            model.l);
        }
Exemplo n.º 22
0
        public void TrainLibSVM(double[][] vektoren, double[] labels, double currentC, double currentG, out int errorCount)
        {
            int         nrdocs = vektoren.Length;
            svm_problem prob   = new svm_problem();

            prob.l = vektoren.Length - 1;
            prob.y = labels;
            svm_node[][] nodes = new svm_node[nrdocs][];

            for (int i = 0; i < vektoren.Length; i++)
            {
                int dim = vektoren[i].Length;

                nodes[i] = new svm_node[dim + 1];

                for (int j = 0; j < dim; j++)
                {
                    svm_node n = new svm_node();
                    n.index         = j;
                    n.value_Renamed = vektoren[i][j];

                    nodes[i][j] = n;
                }
                svm_node ln = new svm_node();
                ln.index         = -1;
                ln.value_Renamed = 0;
                nodes[i][dim]    = ln;
            }

            prob.x = nodes;

            svm_parameter param = new svm_parameter();

            param.cache_size = 256.0;
            param.C          = 1000.0;
            //param.weight = new double[] { 1.0, 1.0, 1.0, 1.0, 1.0 };
            //param.weight_label = new int[] { 1, 1, 1, 1, 1 };
            param.svm_type    = svm_parameter.C_SVC;
            param.kernel_type = svm_parameter.SIGMOID;
            param.gamma       = 0.00000001;
            param.eps         = 0.0001;
            //param.nr_weight = 0;
            param.probability = 1;

            //double[] cs;
            //double[] gs;

            double[] cergs     = new double[labels.Length];
            int      minfehler = labels.Length;
            int      fehler    = 0;
            double   c         = 0.0;
            double   g         = 0.0;

            #region Parameterabstimmung
            //cs = new double[] { Math.Pow(2.0, -15.0), Math.Pow(2.0, -11.0), Math.Pow(2.0, -9.0), Math.Pow(2.0, -7.0), Math.Pow(2.0, -5.0), Math.Pow(2.0, -3.0), Math.Pow(2.0, -1.0), Math.Pow(2.0, 1.0), Math.Pow(2.0, 3.0), Math.Pow(2.0, 5.0), Math.Pow(2.0, 7.0), Math.Pow(2.0, 12.0), Math.Pow(2.0, 15.0) };
            //gs = new double[] { Math.Pow(2.0, -15.0), Math.Pow(2.0, -12.0), Math.Pow(2.0, -9.0), Math.Pow(2.0, -7.0), Math.Pow(2.0, -5.0), Math.Pow(2.0, -3.0), Math.Pow(2.0, -1.0), Math.Pow(2.0, 1.0), Math.Pow(2.0, 3.0) };
            //cs = new double[] { Math.Pow(2.0, -3.0), Math.Pow(2.0, -1.0), Math.Pow(2.0, 1.0), Math.Pow(2.0, 3.0), Math.Pow(2.0, 5.0), Math.Pow(2.0, 7.0), Math.Pow(2.0, 12.0) };
            //gs = new double[] { Math.Pow(2.0, -7.0), Math.Pow(2.0, -5.0), Math.Pow(2.0, -3.0), Math.Pow(2.0, -1.0), Math.Pow(2.0, 1.0), Math.Pow(2.0, 3.0) };

            //for (int i = 0; i < cs.Length; i++)
            //{
            //    param.C = cs[i];

            //    for (int j = 0; j < gs.Length; j++)
            //    {
            //        fehler = 0;
            //        param.gamma = gs[j];
            //        string res = svm.svm_check_parameter(prob, param);
            //        if (res == null)
            //        {
            //            svm.svm_cross_validation(prob, param, vektoren.Length/4, cergs);

            //            for (int k = 0; k < labels.Length; k++)
            //            {
            //                if (cergs[k] != labels[k])
            //                    fehler++;
            //            }
            //            if (fehler < minfehler)
            //            {
            //                minfehler = fehler;
            //                c = param.C;
            //                g = param.gamma;
            //            }
            //        }
            //    }
            //}

            param.C     = currentC;
            fehler      = 0;
            param.gamma = currentG;
            string res = svm.svm_check_parameter(prob, param);
            if (res == null)
            {
                svm.svm_cross_validation(prob, param, vektoren.Length / 4, cergs);

                for (int k = 0; k < labels.Length; k++)
                {
                    if (cergs[k] != labels[k])
                    {
                        fehler++;
                    }
                }
                if (fehler < minfehler)
                {
                    minfehler = fehler;
                    c         = param.C;
                    g         = param.gamma;
                }
            }

            #endregion

            #region Feinabstimmung
            //cs = new double[] { c * 0.3, c * 0.4, c * 0.5, c * 0.6, c * 0.7, c * 0.8, c * 0.9, c, c * 2.0, c * 3.0, c * 4.0, c * 5.0, c * 6.0 };
            //gs = new double[] { g * 0.5, g * 0.6, g * 0.7, g * 0.8, g * 0.9, g, g * 2.0, g * 3.0, g * 4.0 };
            double[] csF = new double[] { c * 0.6, c * 0.7, c * 0.8, c * 0.9, c, c * 2.0, c * 3.0 };
            double[] gsF = new double[] { g * 0.7, g * 0.8, g * 0.9, g, g * 2.0, g * 3.0 };

            for (int i = 0; i < csF.Length; i++)
            {
                param.C = csF[i];

                for (int j = 0; j < gsF.Length; j++)
                {
                    fehler      = 0;
                    param.gamma = gsF[j];
                    res         = svm.svm_check_parameter(prob, param);
                    if (res == null)
                    {
                        svm.svm_cross_validation(prob, param, vektoren.Length / 4, cergs);

                        for (int k = 0; k < labels.Length; k++)
                        {
                            if (cergs[k] != labels[k])
                            {
                                fehler++;
                            }
                        }
                        if (fehler < minfehler)
                        {
                            minfehler = fehler;
                            c         = param.C;
                            g         = param.gamma;
                        }
                    }
                    //Thread.Sleep(1);
                }
                //Thread.Sleep(10);
            }
            #endregion

            #region Superfeinabstimmung
            //cs = new double[] { c - 7.0, c - 6.0, c - 5.0, c - 4.0, c - 3.0, c - 2.0, c - 1.0, c, c + 1.0, c + 2.0, c + 3.0, c + 4.0, c + 5.0 };
            //gs = new double[] { g - 5.0, g - 4.0, g - 3.0, g - 2.0, g - 1.0, g, g + 1.0, g + 2.0, g + 3.0 };

            /*cs = new double[] { c - 1.0, c - 0.3, c - 0.1, c, c + 0.1, c + 0.3, c + 1.0, };
             * gs = new double[] { g - 1.0, g - 0.3, g - 0.1, g, g + 0.1, g + 0.3, g + 1.0 };
             * for (int i = 0; i < cs.Length; i++)
             * {
             *  param.C = cs[i];
             *
             *  for (int j = 0; j < gs.Length; j++)
             *  {
             *      fehler = 0;
             *      param.gamma = gs[j];
             *      string res = svm.svm_check_parameter(prob, param);
             *      if (res == null)
             *      {
             *          svm.svm_cross_validation(prob, param, 6, cergs);
             *
             *          for (int k = 0; k < labels.Length; k++)
             *          {
             *              if (cergs[k] != labels[k])
             *                  fehler++;
             *          }
             *          if (fehler < minfehler)
             *          {
             *              minfehler = fehler;
             *              c = param.C;
             *              g = param.gamma;
             *          }
             *      }
             *  }
             * }*/
            #endregion


            param.C     = c;
            param.gamma = g;

            this._model = new svm_model();
            this._model = svm.svm_train(prob, param);

            int      anzKlassen = svm.svm_get_nr_class(this._model);
            double[] probs      = new double[anzKlassen];

            double erg;
            erg = svm.svm_predict_probability(this._model, nodes[0], probs);
            //erg = svm.svm_predict_probability(this._model, nodes[11], probs);
            //klazzifiziere(this.testvektor);
            //klazzifiziere(vektoren[6]);

            errorCount = minfehler;
        }
Exemplo n.º 23
0
 public SVR(SvmType svm_type, svm_problem prob, Kernel kernel, double C, double eps, bool probability, double cache_size)
     : base(prob, (int)svm_type, kernel, C, 0.0, cache_size, 1e-3, 0.1, 1, probability ? 1 : 0, 0, new int[0], new double[0])
 {
 }
Exemplo n.º 24
0
 public SVC(SvmType svm_type, svm_problem prob, Kernel kernel, double C, double cache_size = 100, int probability = 0)
     : base(prob, (int)svm_type, kernel, C, 0.0, cache_size, 1e-3, 0.1, 1, probability, 0, new int[0], new double[0])
 {
 }
Exemplo n.º 25
0
        public static void svm_cross_validation(svm_problem prob, svm_parameter param, int nr_fold, double[] target)
        {
            int i;
            int[] 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;
                svm_problem 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))
                {
                    double[] 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]]);
            }
        }
Exemplo n.º 26
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)));
 }
Exemplo n.º 27
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;
            double[] minus_ones = new double[l];
            sbyte[] 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] = (sbyte) (+ 1);
                else
                    y[i] = - 1;
            }

            Solver 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)
                //Debug.WriteLine("nu = " + sum_alpha / (Cp * prob.l) + "\n");

            for (i = 0; i < l; i++)
                alpha[i] *= y[i];
        }
Exemplo n.º 28
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;
 }
Exemplo n.º 29
0
        private static void solve_epsilon_svr(svm_problem prob, svm_parameter param, double[] alpha, Solver.SolutionInfo si)
        {
            int l = prob.l;
            double[] alpha2 = new double[2 * l];
            double[] linear_term = new double[2 * l];
            sbyte[] 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;
            }

            Solver 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 += System.Math.Abs(alpha[i]);
            }
            //Debug.WriteLine("nu = " + sum_alpha / (param.C * l) + "\n");
        }
Exemplo n.º 30
0
        //
        // Interface functions
        //
        public static svm_model svm_train(svm_problem prob, svm_parameter param, TrainingProgressEvent progressEvent = null)
        {
            svm_model 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 (System.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 (System.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;
                int[] label = new int[max_nr_class];
                int[] count = new int[max_nr_class];
                int[] 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"'
                    int 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;
                            int[] 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

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

                svm_node[][] 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

                double[] 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)
                        System.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

                bool[] nonzero = new bool[l];
                for (i = 0; i < l; i++)
                    nonzero[i] = false;
                decision_function[] 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++)
                    {
                        svm_problem 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)
                        {
                            double[] 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] && System.Math.Abs(f[p].alpha[k]) > 0)
                                nonzero[si + k] = true;
                        for (k = 0; k < cj; k++)
                            if (!nonzero[sj + k] && System.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;
                int[] 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;
                }

                //Debug.WriteLine("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];

                int[] 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;
        }
Exemplo n.º 31
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;

            sbyte[] y = new sbyte[l];

            for (i = 0; i < l; i++)
                if (prob.y[i] > 0)
                    y[i] = (sbyte) (+ 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] = System.Math.Min(1.0, sum_pos);
                    sum_pos -= alpha[i];
                }
                else
                {
                    alpha[i] = System.Math.Min(1.0, sum_neg);
                    sum_neg -= alpha[i];
                }

            double[] zeros = new double[l];

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

            Solver_NU 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;

            //Debug.WriteLine("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;
        }
Exemplo n.º 32
0
 /// <summary>
 /// Classification SVM
 /// Supports multi-class classification
 /// </summary>
 /// <param name="prob">Training Data Set</param>
 /// <param name="kernel">Selected Kernel</param>
 /// <param name="C">Cost parameter </param>
 /// <param name="cache_size">Indicates the maximum memory that can use the program</param>
 public C_SVC(svm_problem prob, Kernel kernel, double C, double cache_size = 100)
     : base(SvmType.C_SVC, prob, kernel, C, cache_size)
 {
 }
Exemplo n.º 33
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;
            double[] alpha2 = new double[2 * l];
            double[] linear_term = new double[2 * l];
            sbyte[] 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] = System.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;
            }

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

            //Debug.WriteLine("epsilon = " + (- si.r) + "\n");

            for (i = 0; i < l; i++)
                alpha[i] = alpha2[i] - alpha2[i + l];
        }
Exemplo n.º 34
0
	// read in a problem (in svmlight format)
	
	private void  read_problem()
	{
		/* UPGRADE_TODO: Expected value of parameters of constructor
		 * 'java.io.BufferedReader.BufferedReader' are different in the equivalent in .NET.
		 * 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1092"'
		 */
		System.IO.StreamReader fp = new System.IO.StreamReader(input_file_name);
		System.Collections.ArrayList vy = new System.Collections.ArrayList(10);
		System.Collections.ArrayList vx = new System.Collections.ArrayList(10);
		int max_index = 0;
		
		while (true)
		{
			System.String line = fp.ReadLine();
			if ((System.Object) line == null)
				break;
			
			SupportClass.Tokenizer st = new SupportClass.Tokenizer(line, " \t\n\r\f:");
			
			vy.Add(st.NextToken());
			int m = st.Count / 2;
			svm_node[] x = new svm_node[m];
			for (int j = 0; j < m; j++)
			{
				x[j] = new svm_node();
				x[j].index = atoi(st.NextToken());
				x[j].value_Renamed = atof(st.NextToken());
			}
			if (m > 0)
				max_index = System.Math.Max(max_index, x[m - 1].index);
			vx.Add(x);
		}
		
		prob = new svm_problem();
		prob.l = vy.Count;
		prob.x = new svm_node[prob.l][];
		for (int i = 0; i < prob.l; i++)
			prob.x[i] = (svm_node[]) vx[i];
		prob.y = new double[prob.l];
		for (int i = 0; i < prob.l; i++)
			prob.y[i] = atof((System.String) vy[i]);
		
		if (param.gamma == 0)
			param.gamma = 1.0 / max_index;
		
		fp.Close();
	}
Exemplo n.º 35
0
        private static void solve_one_class(svm_problem prob, svm_parameter param, double[] alpha, Solver.SolutionInfo si)
        {
            int l = prob.l;
            double[] zeros = new double[l];
            sbyte[] 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"'
            int 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;
            }

            Solver s = new Solver();
            s.Solve(l, new ONE_CLASS_Q(prob, param), zeros, ones, alpha, 1.0, 1.0, param.eps, si, param.shrinking);
        }