public static void Run(IRegressionProblemData problemData, IEnumerable <string> allowedInputVariables,
                               string svmType, string kernelType, double cost, double nu, double gamma, double epsilon, int degree,
                               out ISupportVectorMachineModel model, out int nSv)
        {
            var               dataset        = problemData.Dataset;
            string            targetVariable = problemData.TargetVariable;
            IEnumerable <int> rows           = problemData.TrainingIndices;

            svm_parameter parameter = new svm_parameter {
                svm_type    = GetSvmType(svmType),
                kernel_type = GetKernelType(kernelType),
                C           = cost,
                nu          = nu,
                gamma       = gamma,
                p           = epsilon,
                cache_size  = 500,
                probability = 0,
                eps         = 0.001,
                degree      = degree,
                shrinking   = 1,
                coef0       = 0
            };

            svm_problem    problem        = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
            RangeTransform rangeTransform = RangeTransform.Compute(problem);
            svm_problem    scaledProblem  = rangeTransform.Scale(problem);
            var            svmModel       = svm.svm_train(scaledProblem, parameter);

            nSv = svmModel.SV.Length;

            model = new SupportVectorMachineModel(svmModel, rangeTransform, targetVariable, allowedInputVariables);
        }
Ejemplo n.º 2
0
        public static void Run(IClassificationProblemData problemData, IEnumerable <string> allowedInputVariables,
                               int svmType, int kernelType, double cost, double nu, double gamma, int degree,
                               out ISupportVectorMachineModel model, out int nSv)
        {
            var               dataset        = problemData.Dataset;
            string            targetVariable = problemData.TargetVariable;
            IEnumerable <int> rows           = problemData.TrainingIndices;

            svm_parameter parameter = new svm_parameter {
                svm_type    = svmType,
                kernel_type = kernelType,
                C           = cost,
                nu          = nu,
                gamma       = gamma,
                cache_size  = 500,
                probability = 0,
                eps         = 0.001,
                degree      = degree,
                shrinking   = 1,
                coef0       = 0
            };

            var weightLabels = new List <int>();
            var weights      = new List <double>();

            foreach (double c in problemData.ClassValues)
            {
                double wSum = 0.0;
                foreach (double otherClass in problemData.ClassValues)
                {
                    if (!c.IsAlmost(otherClass))
                    {
                        wSum += problemData.GetClassificationPenalty(c, otherClass);
                    }
                }
                weightLabels.Add((int)c);
                weights.Add(wSum);
            }
            parameter.weight_label = weightLabels.ToArray();
            parameter.weight       = weights.ToArray();

            svm_problem    problem        = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
            RangeTransform rangeTransform = RangeTransform.Compute(problem);
            svm_problem    scaledProblem  = rangeTransform.Scale(problem);
            var            svmModel       = svm.svm_train(scaledProblem, parameter);

            nSv = svmModel.SV.Length;

            model = new SupportVectorMachineModel(svmModel, rangeTransform, targetVariable, allowedInputVariables, problemData.ClassValues);
        }