Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ptr_model"></param>
        /// <param name="x"></param>
        /// <param name="estimations"></param>
        /// <returns></returns>
        public static double PredictProbability(IntPtr ptr_model, SVMNode[] x, out double[] estimations)
        {
            if (ptr_model == IntPtr.Zero)
            {
                throw new ArgumentNullException("ptr_model");
            }

            bool isProbabilityModel = libsvm.svm_check_probability_model(ptr_model);

            if (!isProbabilityModel)
            {
                SVMModel.Free(ptr_model);
                estimations = null;
                return(-1);
            }

            int classCount = libsvm.svm_get_nr_class(ptr_model);

            IntPtr         ptr_estimations = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * classCount);
            List <SVMNode> nodes           = x.Select(a => a.Clone()).ToList();

            nodes.Add(new SVMNode(-1, 0));
            IntPtr ptr_nodes = SVMNode.Allocate(nodes.ToArray());

            double result = libsvm.svm_predict_probability(ptr_model, ptr_nodes, ptr_estimations);

            estimations = new double[classCount];
            Marshal.Copy(ptr_estimations, estimations, 0, estimations.Length);

            SVMNode.Free(ptr_nodes);
            Marshal.FreeHGlobal(ptr_estimations);
            ptr_estimations = IntPtr.Zero;

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool CheckProbabilityModel(SVMModel model)
        {
            IntPtr ptr_model = SVMModel.Allocate(model);
            bool   success   = libsvm.svm_check_probability_model(ptr_model);

            SVMModel.Free(ptr_model);
            return(success);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// This function does classification or regression on a test vector x given a model.
        /// </summary>
        /// <param name="model">SVM model.</param>
        /// <param name="x">Test vector.</param>
        /// <returns>For a classification model, the predicted class for x is returned.
        /// For a regression model, the function value of x calculated using the model is returned.
        /// For an one-class model, +1 or -1 is returned.</returns>
        public static double Predict(SVMModel model, SVMNode[] x)
        {
            IntPtr ptr_model = SVMModel.Allocate(model);
            double result    = Predict(ptr_model, x);

            SVMModel.Free(ptr_model);
            return(result);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="model"></param>
        /// <param name="x"></param>
        /// <param name="estimations"></param>
        /// <returns></returns>
        public static double PredictProbability(SVMModel model, SVMNode[] x, out double[] estimations)
        {
            IntPtr ptr_model = SVMModel.Allocate(model);
            double result    = PredictProbability(ptr_model, x, out estimations);

            SVMModel.Free(ptr_model);
            return(result);
        }
Ejemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="model"></param>
        /// <param name="x"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public static double PredictValues(SVMModel model, SVMNode[] x, out double[] values)
        {
            IntPtr ptr_model = SVMModel.Allocate(model);
            double result    = PredictValues(ptr_model, x, out values);

            SVMModel.Free(ptr_model);
            return(result);
        }
Ejemplo n.º 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool CheckProbabilityModel(SVMModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            IntPtr ptr_model = SVMModel.Allocate(model);
            bool   success   = libsvm.svm_check_probability_model(ptr_model);

            SVMModel.Free(ptr_model);
            return(success);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// This function does classification or regression on a test vector x given a model.
        /// </summary>
        /// <param name="model">SVM model.</param>
        /// <param name="x">Test vector.</param>
        /// <returns>For a classification model, the predicted class for x is returned.
        /// For a regression model, the function value of x calculated using the model is returned.
        /// For an one-class model, +1 or -1 is returned.</returns>
        public static double Predict(SVMModel model, SVMNode[] x)
        {
            IntPtr         ptr_model = SVMModel.Allocate(model);
            List <SVMNode> nodes     = x.Select(a => a.Clone()).ToList();

            nodes.Add(new SVMNode(-1, 0));
            IntPtr ptr_nodes = SVMNode.Allocate(nodes.ToArray());

            double result = libsvm.svm_predict(ptr_model, ptr_nodes);

            SVMModel.Free(ptr_model);
            SVMNode.Free(ptr_nodes);

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

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

            SVMModel.Free(x);

            Marshal.DestroyStructure(ptr, typeof(svm_model));
            Marshal.FreeHGlobal(ptr);
            ptr = IntPtr.Zero;
        }
Ejemplo n.º 9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="model"></param>
        /// <param name="x"></param>
        /// <param name="estimations"></param>
        /// <returns></returns>
        public static double PredictProbability(SVMModel model, SVMNode[] x, out double[] estimations)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

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

            IntPtr ptr_model = SVMModel.Allocate(model);
            double result    = PredictProbability(ptr_model, x, out estimations);

            SVMModel.Free(ptr_model);
            return(result);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// This function does classification or regression on a test vector x given a model.
        /// </summary>
        /// <param name="model">SVM model.</param>
        /// <param name="x">Test vector.</param>
        /// <returns>For a classification model, the predicted class for x is returned.
        /// For a regression model, the function value of x calculated using the model is returned.
        /// For an one-class model, +1 or -1 is returned.</returns>
        public static double Predict(SVMModel model, SVMNode[] x)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

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

            IntPtr ptr_model = SVMModel.Allocate(model);
            double result    = Predict(ptr_model, x);

            SVMModel.Free(ptr_model);
            return(result);
        }
Ejemplo n.º 11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="model"></param>
        /// <param name="x"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public static double PredictValues(SVMModel model, SVMNode[] x, out double[] values)
        {
            int            size       = (int)(model.ClassCount * (model.ClassCount - 1) * 0.5);
            IntPtr         ptr_values = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * size);
            IntPtr         ptr_model  = SVMModel.Allocate(model);
            List <SVMNode> nodes      = x.Select(a => a.Clone()).ToList();

            nodes.Add(new SVMNode(-1, 0));
            IntPtr ptr_nodes = SVMNode.Allocate(nodes.ToArray());

            double result = libsvm.svm_predict_values(ptr_model, ptr_nodes, ptr_values);

            values = new double[size];
            Marshal.Copy(ptr_values, values, 0, values.Length);

            SVMModel.Free(ptr_model);
            SVMNode.Free(ptr_nodes);
            Marshal.FreeHGlobal(ptr_values);
            ptr_values = IntPtr.Zero;

            return(result);
        }