Esempio n. 1
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);
        }
Esempio n. 2
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);
        }
Esempio n. 3
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);
        }
Esempio n. 4
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);
        }
Esempio n. 5
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);
        }
Esempio n. 6
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);
        }
Esempio n. 7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="model"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static bool SaveModel(SVMModel model, string filename)
        {
            if (String.IsNullOrWhiteSpace(filename) || model == null)
            {
                return(false);
            }

            IntPtr ptr_model    = SVMModel.Allocate(model);
            IntPtr ptr_filename = Marshal.StringToHGlobalAnsi(filename);

            bool success = libsvm.svm_save_model(ptr_filename, ptr_model) == 0;

            Marshal.FreeHGlobal(ptr_filename);
            ptr_filename = IntPtr.Zero;

            return(success);
        }
Esempio n. 8
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);
        }
Esempio n. 9
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);
        }
Esempio n. 10
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);
        }