Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="problem"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public static string CheckParameter(SVMProblem problem, SVMParameter parameter)
        {
            if (problem == null)
            {
                throw new ArgumentNullException("problem");
            }

            if (parameter == null)
            {
                throw new ArgumentNullException("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);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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)
        {
            if (problem == null)
            {
                throw new ArgumentNullException("problem");
            }

            if (parameter == null)
            {
                throw new ArgumentNullException("parameter");
            }

            if (nFolds < 2)
            {
                throw new ArgumentOutOfRangeException("nFolds");
            }

            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;
        }
Ejemplo n.º 4
0
 public OneClassClassifier(List<SVMNode[]> trainingData)
 {
     SVMProblem problem = new SVMProblem();
     for (int i = 0; i < trainingData.Count; i++)
     {
         problem.Add(trainingData[i], 1);
     }
     _trainingData = problem;
 }
Ejemplo n.º 5
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;
 }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        public static SVMProblem Convert(IntPtr ptr)
        {
            if (ptr == IntPtr.Zero)
            {
                return(null);
            }

            svm_problem x = (svm_problem)Marshal.PtrToStructure(ptr, typeof(svm_problem));

            return(SVMProblem.Convert(x));
        }
Ejemplo n.º 8
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)
        {
            IntPtr ptr_problem = SVMProblem.Allocate(problem);
            IntPtr ptr_parameter = SVMParameter.Allocate(parameter);

            IntPtr ptr_model = libsvm.svm_train(ptr_problem, ptr_parameter);
            SVMModel model = SVMModel.Convert(ptr_model);

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

            return model;
        }
Ejemplo n.º 9
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)
        {
            IntPtr ptr_problem   = SVMProblem.Allocate(problem);
            IntPtr ptr_parameter = SVMParameter.Allocate(parameter);

            IntPtr   ptr_model = libsvm.svm_train(ptr_problem, ptr_parameter);
            SVMModel model     = SVMModel.Convert(ptr_model);

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

            return(model);
        }
Ejemplo n.º 10
0
        public static void Free(IntPtr ptr)
        {
            if (ptr == IntPtr.Zero)
            {
                return;
            }

            svm_problem x = (svm_problem)Marshal.PtrToStructure(ptr, typeof(svm_problem));

            SVMProblem.Free(x);

            Marshal.DestroyStructure(ptr, typeof(svm_problem));
            Marshal.FreeHGlobal(ptr);
            ptr = IntPtr.Zero;
        }
Ejemplo n.º 11
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;
        }
Ejemplo n.º 12
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;
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
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);
        }
Ejemplo n.º 15
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;
        }
Ejemplo n.º 16
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)
        {
            if (problem == null)
            {
                throw new ArgumentNullException("problem");
            }

            if (parameter == null)
            {
                throw new ArgumentNullException("parameter");
            }

            IntPtr ptr_problem   = SVMProblem.Allocate(problem);
            IntPtr ptr_parameter = SVMParameter.Allocate(parameter);

            IntPtr   ptr_model = libsvm.svm_train(ptr_problem, ptr_parameter);
            SVMModel model     = SVMModel.Convert(ptr_model);

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

            return(model);
        }
Ejemplo n.º 17
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)
        {
            if (problem == null)
            {
                throw new ArgumentNullException("problem");
            }

            if (parameter == null)
            {
                throw new ArgumentNullException("parameter");
            }

            IntPtr ptr_problem = SVMProblem.Allocate(problem);
            IntPtr ptr_parameter = SVMParameter.Allocate(parameter);

            IntPtr ptr_model = libsvm.svm_train(ptr_problem, ptr_parameter);
            SVMModel model = SVMModel.Convert(ptr_model);

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

            return model;
        }
Ejemplo n.º 18
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="trainingData">The training data which the svm will learn the "normal" data from</param>
 public OneClassClassifier(List<List<double>> trainingData)
 {
     _trainingData = trainingData.CreateCompleteProblemOneClass(); ;
 }
Ejemplo n.º 19
0
 public Clasification()
 {
     problem = SVMProblemHelper.Load(@"C:\Training.txt");
     testProblem = SVMProblemHelper.Load(@"C:\Training.txt");
 }
Ejemplo n.º 20
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;
        }
Ejemplo n.º 21
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;
        }
Ejemplo n.º 22
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="problem"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public static string CheckParameter(SVMProblem problem, SVMParameter parameter)
        {
            if (problem == null)
            {
                throw new ArgumentNullException("problem");
            }

            if (parameter == null)
            {
                throw new ArgumentNullException("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;
        }
Ejemplo n.º 23
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)
        {
            if (problem == null)
            {
                throw new ArgumentNullException("problem");
            }

            if (parameter == null)
            {
                throw new ArgumentNullException("parameter");
            }

            if (nFolds < 2)
            {
                throw new ArgumentOutOfRangeException("nFolds");
            }

            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;
        }
Ejemplo n.º 24
0
 public Clasification()
 {
     problem = SVMProblemHelper.Load(@"C:\Training.txt");
     testProblem = SVMProblemHelper.Load(@"C:\Training.txt");
     System.Diagnostics.Debug.WriteLine("creo el objeto");
 }