public override ClassificationModel Train(BaseVector[] x, int[][] y, int ngroups, Parameters param, int nthreads,
                                                  Action <double> reportProgress)
        {
            string err = CheckInput(x, y, ngroups);

            if (err != null)
            {
                throw new Exception(err);
            }
            ParameterWithSubParams <int> kernelParam = param.GetParamWithSubParams <int>("Kernel");
            SvmParameter sp = new SvmParameter {
                kernelFunction = KernelFunctions.GetKernelFunction(kernelParam.Value, kernelParam.GetSubParameters()),
                svmType        = SvmType.CSvc,
                c = param.GetParam <double>("C").Value
            };

            bool[]            invert;
            SvmProblem[]      problems = CreateProblems(x, y, ngroups, out invert);
            SvmModel[]        models   = new SvmModel[problems.Length];
            ThreadDistributor td       = new ThreadDistributor(nthreads, models.Length,
                                                               i => { models[i] = SvmMain.SvmTrain(problems[i], sp); }, fractionDone => { reportProgress?.Invoke(fractionDone); });

            td.Start();
            return(new SvmClassificationModel(models, invert));
        }
        private static int[] RankBinary(SvmProblem prob, SvmParameter param, double redfactor)
        {
            int nfeatures = prob.x[0].Length;

            int[]      result            = new int[nfeatures];
            int[]      survivingFeatures = ArrayUtils.ConsecutiveInts(nfeatures);
            SvmProblem problem           = prob.Copy();
            int        p = nfeatures - 1;

            int[] indices = ArrayUtils.ConsecutiveInts(nfeatures);
            while (survivingFeatures.Length > 0)
            {
                problem = problem.ExtractFeatures(survivingFeatures);
                indices = ArrayUtils.SubArray(indices, survivingFeatures);
                int      nfeatures2 = survivingFeatures.Length;
                double[] criteria   = ComputeRankingCriteria(SvmMain.SvmTrain(problem, param)
                                                             .ComputeBinaryClassifierWeights(nfeatures2));
                int[] order = ArrayUtils.Order(criteria);
                int   numFeaturesToRemove = Math.Max((int)Math.Round(nfeatures2 / redfactor), 1);
                for (int i = 0; i < numFeaturesToRemove; ++i)
                {
                    result[p--] = indices[order[i]];
                }
                survivingFeatures = ArrayUtils.SubArray(order, numFeaturesToRemove, nfeatures2);
            }
            return(result);
        }
        private static int[] RankBinary(SvmProblem prob, SvmParameter param)
        {
            int nfeatures = prob.x[0].Length;

            double[] criteria = ComputeRankingCriteria(SvmMain.SvmTrain(prob, param).ComputeBinaryClassifierWeights(nfeatures));
            int[]    order    = ArrayUtils.Order(criteria);
            Array.Reverse(order);
            return(order);
        }
        public RegressionModel Train(BaseVector[] x, float[] y, Parameters param, int nthreads)
        {
            ParameterWithSubParams <int> kernelParam = param.GetParamWithSubParams <int>("Kernel");
            SvmParameter sp = new SvmParameter {
                kernelFunction = KernelFunctions.GetKernelFunction(kernelParam.Value, kernelParam.GetSubParameters()),
                svmType        = SvmType.EpsilonSvr,
                c = param.GetParam <double>("C").Value
            };
            SvmModel model = SvmMain.SvmTrain(new SvmProblem(x, y), sp);

            return(new SvmRegressionModel(model));
        }
Exemple #5
0
        public override RegressionModel Train(BaseVector[] x, int[] nominal, double[] y, Parameters param, int nthreads, Action <double> reportProgress)
        {
            x = ClassificationMethod.ToOneHotEncoding(x, nominal);
            ParameterWithSubParams <int> kernelParam = param.GetParamWithSubParams <int>("Kernel");
            SvmParameter sp = new SvmParameter {
                kernelFunction = KernelFunctions.GetKernelFunction(kernelParam.Value, kernelParam.GetSubParameters()),
                svmType        = SvmType.EpsilonSvr,
                c = param.GetParam <double>("C").Value
            };
            SvmModel model = SvmMain.SvmTrain(new SvmProblem(x, y), sp);

            return(new SvmRegressionModel(model));
        }