예제 #1
0
파일: SVM.cs 프로젝트: oloopy/LibSVMsharp
        /// <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);
        }
예제 #2
0
파일: SVM.cs 프로젝트: oloopy/LibSVMsharp
        /// <summary>
        ///
        /// </summary>
        /// <param name="ptr_model"></param>
        /// <param name="x"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public static double PredictValues(IntPtr ptr_model, SVMNode[] x, out double[] values)
        {
            if (ptr_model == IntPtr.Zero)
            {
                throw new ArgumentNullException("ptr_model");
            }

            int    classCount = libsvm.svm_get_nr_class(ptr_model);
            int    size       = (int)(classCount * (classCount - 1) * 0.5);
            IntPtr ptr_values = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * size);

            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);

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

            return(result);
        }
예제 #3
0
        public static void Free(svm_model x)
        {
            Marshal.FreeHGlobal(x.rho);
            x.rho = IntPtr.Zero;

            Marshal.FreeHGlobal(x.probA);
            x.probA = IntPtr.Zero;

            Marshal.FreeHGlobal(x.probB);
            x.probB = IntPtr.Zero;

            Marshal.FreeHGlobal(x.sv_indices);
            x.sv_indices = IntPtr.Zero;

            Marshal.FreeHGlobal(x.label);
            x.label = IntPtr.Zero;

            Marshal.FreeHGlobal(x.nSV);
            x.nSV = IntPtr.Zero;

            SVMParameter.Free(x.param);

            IntPtr i_ptr_sv = x.SV;

            for (int i = 0; i < x.l; i++)
            {
                IntPtr ptr_nodes = (IntPtr)Marshal.PtrToStructure(i_ptr_sv, typeof(IntPtr));
                SVMNode.Free(ptr_nodes);

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

            Marshal.FreeHGlobal(x.SV);
            x.SV = IntPtr.Zero;

            IntPtr i_ptr_svcoef = x.sv_coef;

            for (int i = 0; i < x.nr_class - 1; i++)
            {
                IntPtr temp = (IntPtr)Marshal.PtrToStructure(i_ptr_svcoef, typeof(IntPtr));
                Marshal.FreeHGlobal(temp);
                temp = IntPtr.Zero;

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

            Marshal.FreeHGlobal(x.sv_coef);
            x.sv_coef = IntPtr.Zero;
        }
예제 #4
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);
        }
예제 #5
0
파일: SVM.cs 프로젝트: oloopy/LibSVMsharp
        /// <summary>
        ///
        /// </summary>
        /// <param name="ptr_model"></param>
        /// <param name="x"></param>
        /// <returns></returns>
        public static double Predict(IntPtr ptr_model, SVMNode[] x)
        {
            if (ptr_model == IntPtr.Zero)
            {
                throw new ArgumentNullException("ptr_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);

            SVMNode.Free(ptr_nodes);

            return(result);
        }
예제 #6
0
        public static void Free(svm_problem x)
        {
            Marshal.FreeHGlobal(x.y);
            x.y = IntPtr.Zero;

            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.Free(ptr_nodes);

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

            Marshal.FreeHGlobal(x.x);
            x.x = IntPtr.Zero;
        }