当前位置: 首页>>代码示例>>C#>>正文


C# SVM.Parameter类代码示例

本文整理汇总了C#中SVM.Parameter的典型用法代码示例。如果您正苦于以下问题:C# Parameter类的具体用法?C# Parameter怎么用?C# Parameter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Parameter类属于SVM命名空间,在下文中一共展示了Parameter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: PerformCrossValidation

 /// <summary>
 /// Performs cross validation.
 /// </summary>
 /// <param name="problem">The training data</param>
 /// <param name="parameters">The parameters to test</param>
 /// <param name="nrfold">The number of cross validations to use</param>
 /// <returns>The cross validation score</returns>
 public static double PerformCrossValidation(Problem problem, Parameter parameters, int nrfold)
 {
     string error = Procedures.svm_check_parameter(problem, parameters);
     if (error == null)
         return doCrossValidation(problem, parameters, nrfold);
     else throw new Exception(error);
 }
开发者ID:hksonngan,项目名称:mytesgnikrow,代码行数:14,代码来源:Training.cs

示例2: Main

        public static void Main(string[] args)
        {
            Problem train = Problem.Read("a1a.train.txt");
            Problem test = Problem.Read("a1a.test.txt");

            //For this example (and indeed, many scenarios), the default
            //parameters will suffice.
            Parameter parameters = new Parameter();
            double C;
            double Gamma;

            //This will do a grid optimization to find the best parameters
            //and store them in C and Gamma, outputting the entire
            //search to params.txt.

            ParameterSelection.Grid(train, parameters, "params.txt", out C, out Gamma);
            parameters.C = C;
            parameters.Gamma = Gamma;

            //Train the model using the optimal parameters.

            Model model = Training.Train(train, parameters);

            //Perform classification on the test data, putting the
            //results in results.txt.

            Prediction.Predict(test, "results.txt", model, false);
        }
开发者ID:orlovk,项目名称:PtProject,代码行数:28,代码来源:Program.cs

示例3: PrecomputedKernel

 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="rows">Nodes to use as the rows of the matrix</param>
 /// <param name="columns">Nodes to use as the columns of the matrix</param>
 /// <param name="param">Parameters to use when compute similarities</param>
 public PrecomputedKernel(List<Node[]> rows, List<Node[]> columns, Parameter param)
 {
     _rows = rows.Count;
     _columns = columns.Count;
     _similarities = new float[_rows, _columns];
     for (int r = 0; r < _rows; r++)
         for (int c = 0; c < _columns; c++)
             _similarities[r, c] = (float)Kernel.KernelFunction(rows[r], columns[c], param);
 }
开发者ID:hksonngan,项目名称:mytesgnikrow,代码行数:15,代码来源:PrecomputedKernel.cs

示例4: Kernel

        public Kernel(int l, Node[][] x_, Parameter param)
        {
            _kernelType = param.KernelType;
              _degree = param.Degree;
              _gamma = param.Gamma;
              _coef0 = param.Coefficient0;

              _x = (Node[][])x_.Clone();

              if (_kernelType == KernelType.RBF) {
            _xSquare = new double[l];
            for (int i = 0; i < l; i++)
              _xSquare[i] = dot(_x[i], _x[i]);
              } else _xSquare = null;
        }
开发者ID:orlovk,项目名称:PtProject,代码行数:15,代码来源:Kernel.cs

示例5: train

        public Model train(Problem issue)
        {
            var span = Overseer.observe("Training.Parameter-Choosing");
            Parameter parameters = new Parameter();
            parameters.KernelType = KernelType.RBF;
            double C;
            double Gamma;

            ParameterSelection.Grid(issue, parameters, null, out C, out Gamma);
            parameters.C = C;
            parameters.Gamma = Gamma;
            span.die();
            span = Overseer.observe("Training.Training");
            var result = Training.Train(issue, parameters);
            span.die();
            return result;
        }
开发者ID:Termina1,项目名称:diploma-svm-face-project,代码行数:17,代码来源:Trainer.cs

示例6: KernelFunction

 public static double KernelFunction(Node[] x, Node[] y, Parameter param)
 {
     switch (param.KernelType) {
     case KernelType.LINEAR:
       return dot(x, y);
     case KernelType.POLY:
       return powi(param.Degree * dot(x, y) + param.Coefficient0, param.Degree);
     case KernelType.RBF: {
     double sum = computeSquaredDistance(x, y);
     return Math.Exp(-param.Gamma * sum);
       }
     case KernelType.SIGMOID:
       return Math.Tanh(param.Gamma * dot(x, y) + param.Coefficient0);
     case KernelType.PRECOMPUTED:
       return x[(int)(y[0].Value)].Value;
     default:
       return 0;
       }
 }
开发者ID:orlovk,项目名称:PtProject,代码行数:19,代码来源:Kernel.cs

示例7: LearnAttributeToFactorMapping

        ///
        public override void LearnAttributeToFactorMapping()
        {
            var svm_features = new List<Node[]>();
            var relevant_items  = new List<int>();
            for (int i = 0; i < MaxItemID + 1; i++)
            {
                // ignore items w/o collaborative data
                if (Feedback.ItemMatrix[i].Count == 0)
                    continue;
                // ignore items w/o attribute data
                if (item_attributes[i].Count == 0)
                    continue;

                svm_features.Add( CreateNodes(i) );
                relevant_items.Add(i);
            }

            // TODO proper random seed initialization

            Node[][] svm_features_array = svm_features.ToArray();
            var svm_parameters = new Parameter();
            svm_parameters.SvmType = SvmType.EPSILON_SVR;
            //svm_parameters.SvmType = SvmType.NU_SVR;
            svm_parameters.C     = this.c;
            svm_parameters.Gamma = this.gamma;

            models = new Model[num_factors];
            for (int f = 0; f < num_factors; f++)
            {
                double[] targets = new double[svm_features.Count];
                for (int i = 0; i < svm_features.Count; i++)
                {
                    int item_id = relevant_items[i];
                    targets[i] = item_factors[item_id, f];
                }

                Problem svm_problem = new Problem(svm_features.Count, targets, svm_features_array, NumItemAttributes - 1);
                models[f] = SVM.Training.Train(svm_problem, svm_parameters);
            }

            _MapToLatentFactorSpace = Utils.Memoize<int, double[]>(__MapToLatentFactorSpace);
        }
开发者ID:zenogantner,项目名称:MML-KDD,代码行数:43,代码来源:BPRMF_ItemMappingSVR.cs

示例8: TrainAndTest

 private double TrainAndTest(string trainSet,string testSet, string resultFile)
 {
     Problem train = Problem.Read(trainSet);
     Problem test = Problem.Read(testSet);
     Parameter parameters = new Parameter();
     if (chClassification.Checked)
     {
         parameters.SvmType = SvmType.C_SVC;
         parameters.C = 0.03;
         parameters.Gamma = 0.008;
     }
     else
     {
         parameters.SvmType = SvmType.EPSILON_SVR;
         parameters.C = 8;
         parameters.Gamma = 0.063;
         parameters.P = 0.5;
     }
     Model model = Training.Train(train, parameters);
     return Prediction.Predict(test, resultFile, model, true);
 }
开发者ID:aucan,项目名称:IronicSA,代码行数:21,代码来源:Form1.cs

示例9: ONE_CLASS_Q

 public ONE_CLASS_Q(Problem prob, Parameter param)
     : base(prob.Count, prob.X, param)
 {
     cache = new Cache(prob.Count, (long)(param.CacheSize * (1 << 20)));
     QD = new float[prob.Count];
     for (int i = 0; i < prob.Count; i++)
         QD[i] = (float)KernelFunction(i, i);
 }
开发者ID:wendelad,项目名称:RecSys,代码行数:8,代码来源:Solver.cs

示例10: svm_cross_validation

        // Stratified cross validation
        public static void svm_cross_validation(Problem prob, Parameter param, int nr_fold, double[] target)
        {
            Random rand = new Random();
            int i;
            int[] fold_start = new int[nr_fold + 1];
            int l = prob.Count;
            int[] perm = new int[l];

            // stratified cv may not give leave-one-out rate
            // Each class to l folds -> some folds may have zero elements
            if ((param.SvmType == SvmType.C_SVC ||
                param.SvmType == SvmType.NU_SVC) && nr_fold < l)
            {
                int[] tmp_nr_class = new int[1];
                int[][] tmp_label = new int[1][];
                int[][] tmp_start = new int[1][];
                int[][] tmp_count = new int[1][];

                svm_group_classes(prob, tmp_nr_class, tmp_label, tmp_start, tmp_count, perm);

                int nr_class = tmp_nr_class[0];
                //int[] label = tmp_label[0];
                int[] start = tmp_start[0];
                int[] count = tmp_count[0];

                // random shuffle and then data grouped by fold using the array perm
                int[] fold_count = new int[nr_fold];
                int c;
                int[] index = new int[l];
                for (i = 0; i < l; i++)
                    index[i] = perm[i];
                for (c = 0; c < nr_class; c++)
                    for (i = 0; i < count[c]; i++)
                    {
                        int j = i + (int)(rand.NextDouble() * (count[c] - i));
                        do { int _ = index[start[c] + j]; index[start[c] + j] = index[start[c] + i]; index[start[c] + i] = _; } while (false);
                    }
                for (i = 0; i < nr_fold; i++)
                {
                    fold_count[i] = 0;
                    for (c = 0; c < nr_class; c++)
                        fold_count[i] += (i + 1) * count[c] / nr_fold - i * count[c] / nr_fold;
                }
                fold_start[0] = 0;
                for (i = 1; i <= nr_fold; i++)
                    fold_start[i] = fold_start[i - 1] + fold_count[i - 1];
                for (c = 0; c < nr_class; c++)
                    for (i = 0; i < nr_fold; i++)
                    {
                        int begin = start[c] + i * count[c] / nr_fold;
                        int end = start[c] + (i + 1) * count[c] / nr_fold;
                        for (int j = begin; j < end; j++)
                        {
                            perm[fold_start[i]] = index[j];
                            fold_start[i]++;
                        }
                    }
                fold_start[0] = 0;
                for (i = 1; i <= nr_fold; i++)
                    fold_start[i] = fold_start[i - 1] + fold_count[i - 1];
            }
            else
            {
                for (i = 0; i < l; i++) perm[i] = i;
                for (i = 0; i < l; i++)
                {
                    int j = i + (int)(rand.NextDouble() * (l - i));
                    do { int _ = perm[i]; perm[i] = perm[j]; perm[j] = _; } while (false);
                }
                for (i = 0; i <= nr_fold; i++)
                    fold_start[i] = i * l / nr_fold;
            }

            for (i = 0; i < nr_fold; i++)
            {
                int begin = fold_start[i];
                int end = fold_start[i + 1];
                int j, k;
                Problem subprob = new Problem();

                subprob.Count = l - (end - begin);
                subprob.X = new Node[subprob.Count][];
                subprob.Y = new double[subprob.Count];

                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 < l; j++)
                {
                    subprob.X[k] = prob.X[perm[j]];
                    subprob.Y[k] = prob.Y[perm[j]];
                    ++k;
                }
                Model submodel = svm_train(subprob, param);
                if (param.Probability &&
//.........这里部分代码省略.........
开发者ID:wendelad,项目名称:RecSys,代码行数:101,代码来源:Solver.cs

示例11: svm_train_one

        static decision_function svm_train_one(Problem prob, Parameter param, double Cp, double Cn)
        {
            double[] alpha = new double[prob.Count];
            Solver.SolutionInfo si = new Solver.SolutionInfo();
            switch (param.SvmType)
            {
                case SvmType.C_SVC:
                    solve_c_svc(prob, param, alpha, si, Cp, Cn);
                    break;
                case SvmType.NU_SVC:
                    solve_nu_svc(prob, param, alpha, si);
                    break;
                case SvmType.ONE_CLASS:
                    solve_one_class(prob, param, alpha, si);
                    break;
                case SvmType.EPSILON_SVR:
                    solve_epsilon_svr(prob, param, alpha, si);
                    break;
                case SvmType.NU_SVR:
                    solve_nu_svr(prob, param, alpha, si);
                    break;
            }

            Procedures.info("obj = " + si.obj + ", rho = " + si.rho + "\n");

            // output SVs

            int nSV = 0;
            int nBSV = 0;
            for (int i = 0; i < prob.Count; 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;
                    }
                }
            }

            Procedures.info("nSV = " + nSV + ", nBSV = " + nBSV + "\n");

            decision_function f = new decision_function();
            f.alpha = alpha;
            f.rho = si.rho;
            return f;
        }
开发者ID:wendelad,项目名称:RecSys,代码行数:54,代码来源:Solver.cs

示例12: Grid

 /// <summary>
 /// Performs a Grid parameter selection, trying all possible combinations of the two lists and returning the
 /// combination which performed best.  Uses the default values of C and Gamma.
 /// </summary>
 /// <param name="problem">The training data</param>
 /// <param name="validation">The validation data</param>
 /// <param name="parameters">The parameters to use when optimizing</param>
 /// <param name="outputFile">The output file for the parameter results</param>
 /// <param name="C">The optimal C value will be placed in this variable</param>
 /// <param name="Gamma">The optimal Gamma value will be placed in this variable</param>
 public static void Grid(
     Problem problem,
     Problem validation,
     Parameter parameters,
     string outputFile,
     out double C,
     out double Gamma)
 {
     Grid(problem, validation, parameters, GetList(MIN_C, MAX_C, C_STEP), GetList(MIN_G, MAX_G, G_STEP), outputFile, out C, out Gamma);
 }
开发者ID:Termina1,项目名称:libsvm-csharp,代码行数:20,代码来源:ParameterSelection.cs

示例13: solve_nu_svc

        private static void solve_nu_svc(Problem prob, Parameter param,
                        double[] alpha, Solver.SolutionInfo si)
        {
            int i;
            int l = prob.Count;
            double nu = param.Nu;

            sbyte[] 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];
                }

            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;

            Procedures.info("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;
        }
开发者ID:wendelad,项目名称:RecSys,代码行数:49,代码来源:Solver.cs

示例14: solve_c_svc

        private static void solve_c_svc(Problem prob, Parameter param,
                        double[] alpha, Solver.SolutionInfo si,
                        double Cp, double Cn)
        {
            int l = prob.Count;
            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] = +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)
                Procedures.info("nu = " + sum_alpha / (Cp * prob.Count) + "\n");

            for (i = 0; i < l; i++)
                alpha[i] *= y[i];
        }
开发者ID:wendelad,项目名称:RecSys,代码行数:31,代码来源:Solver.cs

示例15: svm_train

        //
        // Interface functions
        //
        public static Model svm_train(Problem prob, Parameter param)
        {
            Model model = new Model();
            model.Parameter = param;

            if (param.SvmType == SvmType.ONE_CLASS ||
               param.SvmType == SvmType.EPSILON_SVR ||
               param.SvmType == SvmType.NU_SVR)
            {
                // regression or one-class-svm
                model.NumberOfClasses = 2;
                model.ClassLabels = null;
                model.NumberOfSVPerClass = null;
                model.PairwiseProbabilityA = null; model.PairwiseProbabilityB = null;
                model.SupportVectorCoefficients = new double[1][];

                if (param.Probability &&
                   (param.SvmType == SvmType.EPSILON_SVR ||
                    param.SvmType == SvmType.NU_SVR))
                {
                    model.PairwiseProbabilityA = new double[1];
                    model.PairwiseProbabilityA[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.Count; i++)
                    if (Math.Abs(f.alpha[i]) > 0) ++nSV;
                model.SupportVectorCount = nSV;
                model.SupportVectors = new Node[nSV][];
                model.SupportVectorCoefficients[0] = new double[nSV];
                int j = 0;
                for (i = 0; i < prob.Count; i++)
                    if (Math.Abs(f.alpha[i]) > 0)
                    {
                        model.SupportVectors[j] = prob.X[i];
                        model.SupportVectorCoefficients[0][j] = f.alpha[i];
                        ++j;
                    }
            }
            else
            {
                // classification
                int l = prob.Count;
                int[] tmp_nr_class = new int[1];
                int[][] tmp_label = new int[1][];
                int[][] tmp_start = new int[1][];
                int[][] tmp_count = new int[1][];
                int[] perm = new int[l];

                // group training data of the same class
                svm_group_classes(prob, tmp_nr_class, tmp_label, tmp_start, tmp_count, perm);
                int nr_class = tmp_nr_class[0];
                int[] label = tmp_label[0];
                int[] start = tmp_start[0];
                int[] count = tmp_count[0];
                Node[][] x = new Node[l][];
                int i;
                for (i = 0; i < l; i++)
                    x[i] = prob.X[perm[i]];

                // calculate weighted C

                double[] weighted_C = new double[nr_class];
                for (i = 0; i < nr_class; i++)
                    weighted_C[i] = param.C;
                foreach (int weightedLabel in param.Weights.Keys)
                {
                    int index = Array.IndexOf<int>(label, weightedLabel);
                    if (index < 0)
                        Console.Error.WriteLine("warning: class label " + weightedLabel + " specified in weight is not found");
                    else weighted_C[index] *= param.Weights[weightedLabel];
                }

                // 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)
                {
                    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++)
                    {
                        Problem sub_prob = new Problem();
//.........这里部分代码省略.........
开发者ID:wendelad,项目名称:RecSys,代码行数:101,代码来源:Solver.cs


注:本文中的SVM.Parameter类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。