Пример #1
0
 public SVMModel()
 {
     Parameter = new SVMParameter();
     ClassCount = 0;
     TotalSVCount = 0;
     SV = null;
     SVCoefs = null;
     Rho = null;
     ProbabilityA = null;
     ProbabilityB = null;
     SVIndices = null;
     Labels = null;
     SVCounts = null;
     Creation = CreationType.LOAD_MODEL;
 }
Пример #2
0
        /// <summary>
        /// This function constructs and returns an SVM model according to the given training data and parameters.
        /// </summary>
        /// <param name="problem">Training data.</param>
        /// <param name="parameter">Parameter set.</param>
        /// <returns>SVM model according to the given training data and parameters.</returns>
        public static SVMModel Train(SVMProblem problem, SVMParameter parameter)
        {
            ///System.Windows.MessageBox.Show("Algo real");
            System.Diagnostics.Debug.WriteLine("hooola");
            IntPtr ptr_problem = SVMProblem.Allocate(problem);
            System.Diagnostics.Debug.WriteLine("hooola 2");
            IntPtr ptr_parameter = SVMParameter.Allocate(parameter);
            System.Diagnostics.Debug.WriteLine("hooola 3");
            IntPtr ptr_model = libsvm.svm_train(ptr_problem, ptr_parameter);
            System.Diagnostics.Debug.WriteLine("hooola 4");
            SVMModel model = SVMModel.Convert(ptr_model);
            System.Diagnostics.Debug.WriteLine("hooola 5");

            SVMProblem.Free(ptr_problem);
            SVMParameter.Free(ptr_parameter);
            libsvm.svm_free_model_content(ptr_model);

            return model;
        }
Пример #3
0
        /*public Classification(string training, string test)
        {
            problem = SVMProblemHelper.Load(training);
            testProblem = SVMProblemHelper.Load(test);
        }*/

        public void ClasificationHand()
        {

            parameter = new SVMParameter();

            parameter.Type = SVMType.C_SVC;
            parameter.Kernel = SVMKernelType.RBF;
            parameter.C = 1;
            parameter.Gamma = 1;


           // System.Windows.MessageBox.Show("Algo real");
            SVMModel model = SVM.Train(problem, parameter);
            System.Windows.MessageBox.Show("Oscar");

            double[] target;
            target = new double[testProblem.Length];
            int a = 3;
            for (int i = 0; i < testProblem.Length; i++)
                target[i] = SVM.Predict(model, testProblem.X[i]);

            double accuracy = SVMHelper.EvaluateClassificationProblem(testProblem, target);
        }
Пример #4
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="problem"></param>
        /// <param name="parameter"></param>
        /// <param name="nFolds"></param>
        /// <param name="target"></param>
        public static void CrossValidation(SVMProblem problem, SVMParameter parameter, int nFolds, out double[] target)
        {
            IntPtr ptr_target = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * problem.Length);
            IntPtr ptr_problem = SVMProblem.Allocate(problem);
            IntPtr ptr_parameter = SVMParameter.Allocate(parameter);

            libsvm.svm_cross_validation(ptr_problem, ptr_parameter, nFolds, ptr_target);

            target = new double[problem.Length];
            Marshal.Copy(ptr_target, target, 0, target.Length);

            SVMProblem.Free(ptr_problem);
            SVMParameter.Free(ptr_parameter);
            Marshal.FreeHGlobal(ptr_target);
            ptr_target = IntPtr.Zero;
        }
Пример #5
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="problem"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public static string CheckParameter(SVMProblem problem, SVMParameter parameter)
        {
            IntPtr ptr_problem = SVMProblem.Allocate(problem);
            IntPtr ptr_parameter = SVMParameter.Allocate(parameter);

            IntPtr ptr_output = libsvm.svm_check_parameter(ptr_problem, ptr_parameter);

            SVMProblem.Free(ptr_problem);
            SVMParameter.Free(ptr_parameter);

            string output = Marshal.PtrToStringAnsi(ptr_output);
            Marshal.FreeHGlobal(ptr_output);
            ptr_output = IntPtr.Zero;

            return output;
        }
Пример #6
0
 public SVMParameter Clone()
 {
     SVMParameter y = new SVMParameter();
     y.Type = Type;
     y.Kernel = Kernel;
     y.Degree = Degree;
     y.Gamma = Gamma;
     y.Coef0 = Coef0;
     y.C = C;
     y.Nu = Nu;
     y.P = P;
     y.CacheSize = CacheSize;
     y.Eps = Eps;
     y.Shrinking = Shrinking;
     y.Probability = Probability;
     y.WeightLabels = WeightLabels.Select(a => a).ToArray();
     y.Weights = Weights.Select(a => a).ToArray();
     return y;
 }
Пример #7
0
        public static IntPtr Allocate(SVMParameter x)
        {
            if (x == null)
                return IntPtr.Zero;

            svm_parameter y = new svm_parameter();
            y.svm_type = (int)x.Type;
            y.kernel_type = (int)x.Kernel;
            y.degree = x.Degree;
            y.gamma = x.Gamma;
            y.coef0 = x.Coef0;
            y.cache_size = x.CacheSize;
            y.eps = x.Eps;
            y.C = x.C;
            y.nu = x.Nu;
            y.p = x.P;
            y.shrinking = x.Shrinking ? 1 : 0;
            y.probability = x.Probability ? 1 : 0;
            y.nr_weight = x.WeightLabels.Length;

            y.weight_label = IntPtr.Zero;
            if (y.nr_weight > 0)
            {
                y.weight_label = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * x.WeightLabels.Length);
                Marshal.Copy(x.WeightLabels, 0, y.weight_label, x.WeightLabels.Length);
            }

            y.weight = IntPtr.Zero;
            if (y.nr_weight > 0)
            {
                y.weight = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * x.Weights.Length);
                Marshal.Copy(x.Weights, 0, y.weight, x.Weights.Length);
            }

            int size = Marshal.SizeOf(y);
            IntPtr ptr = Marshal.AllocHGlobal(size);
            Marshal.StructureToPtr(y, ptr, true);

            return ptr;
        }
Пример #8
0
        public static SVMParameter Convert(svm_parameter x)
        {
            SVMParameter y = new SVMParameter();
            y.Type = (SVMType)x.svm_type;
            y.Kernel = (SVMKernelType)x.kernel_type;
            y.Degree = x.degree;
            y.Gamma = x.gamma;
            y.Coef0 = x.coef0;
            y.CacheSize = x.cache_size;
            y.Eps = x.eps;
            y.C = x.C;
            y.Nu = x.nu;
            y.P = x.p;
            y.Shrinking = x.shrinking != 0;
            y.Probability = x.probability != 0;

            int length = x.nr_weight;
            y.WeightLabels = new int[length];
            if (length > 0)
                Marshal.Copy(x.weight_label, y.WeightLabels, 0, length);

            y.Weights = new double[length];
            if (length > 0)
                Marshal.Copy(x.weight, y.Weights, 0, length);

            return y;
        }