コード例 #1
0
 public SVMProblem Clone()
 {
     SVMProblem y = new SVMProblem();
     for (int i = 0; i < Length; i++)
     {
         SVMNode[] nodes = X[i].Select(x => x.Clone()).ToArray();
         y.Add(nodes, Y[i]);
     }
     return y;
 }
コード例 #2
0
ファイル: SVM.cs プロジェクト: americamm/SystemVersions
        /// <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 static SVMProblem Convert(svm_problem x)
        {
            double[] y_array = new double[x.l];
            Marshal.Copy(x.y, y_array, 0, y_array.Length);

            List<SVMNode[]> x_array = new List<SVMNode[]>();
            IntPtr i_ptr_x = x.x;
            for (int i = 0; i < x.l; i++)
            {
                IntPtr ptr_nodes = (IntPtr)Marshal.PtrToStructure(i_ptr_x, typeof(IntPtr));
                SVMNode[] nodes = SVMNode.Convert(ptr_nodes);
                x_array.Add(nodes);
                i_ptr_x = IntPtr.Add(i_ptr_x, Marshal.SizeOf(typeof(IntPtr)));
            }

            SVMProblem y = new SVMProblem();
            for (int i = 0; i < x.l; i++)
            {
                y.Add(x_array[i], y_array[i]);
            }

            return y;
        }
コード例 #4
0
ファイル: SVM.cs プロジェクト: americamm/SystemVersions
        /// <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
ファイル: SVM.cs プロジェクト: americamm/SystemVersions
        /// <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 static IntPtr Allocate(SVMProblem x)
        {
            if (x == null || x.X == null || x.Y == null || x.Length < 1)
            {
                return IntPtr.Zero;
            }

            svm_problem y = new svm_problem();
            y.l = x.Length;

            // Allocate problem.y
            y.y = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * x.Y.Count);
            Marshal.Copy(x.Y.ToArray(), 0, y.y, x.Y.Count);

            // Allocate problem.x
            y.x = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)) * x.X.Count);
            IntPtr i_ptr_x = y.x;
            for (int i = 0; i < x.X.Count; i++)
            {
                // Prepare each node array 
                // 1) All nodes containing zero value is removed 
                // 2) A node which index is -1 is added to the end
                List<SVMNode> temp = x.X[i].Where(a => a.Value != 0).ToList();
                temp.Add(new SVMNode(-1, 0));
                SVMNode[] nodes = temp.ToArray();

                // Allocate node array
                IntPtr ptr_nodes = SVMNode.Allocate(nodes);
                Marshal.StructureToPtr(ptr_nodes, i_ptr_x, true);

                i_ptr_x = IntPtr.Add(i_ptr_x, Marshal.SizeOf(typeof(IntPtr)));
            }

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

            return ptr;
        }
コード例 #7
0
 public Classification()
 {
     problem = SVMProblemHelper.Load(@"C:\Training.txt");
     testProblem = SVMProblemHelper.Load(@"C:\Training.txt");
 }