/// <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); }
/// <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); }
/// <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); }
/// <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); }
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); }
public static IntPtr Allocate(SVMModel x) { if (x == null || x.ClassCount < 1 || x.Labels == null || x.Labels.Length < 1 || x.Parameter == null || x.Rho == null || x.Rho.Length < 1 || x.SVCoefs == null || x.SVCoefs.Count < 1 || x.TotalSVCount < 1 || x.SVCounts == null || x.SVCounts.Length < 1) { return(IntPtr.Zero); } svm_model y = new svm_model(); y.nr_class = x.ClassCount; y.l = x.TotalSVCount; y.free_sv = (int)x.Creation; // Allocate model.parameter IntPtr ptr_param = SVMParameter.Allocate(x.Parameter); y.param = (svm_parameter)Marshal.PtrToStructure(ptr_param, typeof(svm_parameter)); // Allocate model.rho y.rho = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * x.Rho.Length); Marshal.Copy(x.Rho, 0, y.rho, x.Rho.Length); // Allocate model.probA y.probA = IntPtr.Zero; if (x.ProbabilityA != null) { y.probA = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * x.ProbabilityA.Length); Marshal.Copy(x.ProbabilityA, 0, y.probA, x.ProbabilityA.Length); } // Allocate model.probB y.probB = IntPtr.Zero; if (x.ProbabilityB != null) { y.probB = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * x.ProbabilityB.Length); Marshal.Copy(x.ProbabilityB, 0, y.probB, x.ProbabilityB.Length); } // Allocate model.label y.label = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * x.Labels.Length); Marshal.Copy(x.Labels, 0, y.label, x.Labels.Length); // Allocate model.nSV y.nSV = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * x.SVCounts.Length); Marshal.Copy(x.SVCounts, 0, y.nSV, x.SVCounts.Length); // Allocate model.sv_coef y.sv_coef = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)) * x.SVCoefs.Count); IntPtr i_ptr_svcoef = y.sv_coef; for (int i = 0; i < x.SVCoefs.Count; i++) { IntPtr temp = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * x.SVCoefs[i].Length); Marshal.Copy(x.SVCoefs[i], 0, temp, x.SVCoefs[i].Length); Marshal.StructureToPtr(temp, i_ptr_svcoef, true); i_ptr_svcoef = IntPtr.Add(i_ptr_svcoef, Marshal.SizeOf(typeof(IntPtr))); } // Allocate model.sv_indices y.sv_indices = IntPtr.Zero; if (x.SVIndices != null) { y.sv_indices = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * x.SVIndices.Length); Marshal.Copy(x.SVIndices, 0, y.sv_indices, x.SVIndices.Length); } // Allocate model.SV y.SV = IntPtr.Zero; if (x.SV != null) { y.SV = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)) * x.SV.Count); IntPtr i_ptr_sv = y.SV; for (int i = 0; i < x.SV.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.SV[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_sv, true); i_ptr_sv = IntPtr.Add(i_ptr_sv, Marshal.SizeOf(typeof(IntPtr))); } } // Allocate the model int size = Marshal.SizeOf(y); IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(y, ptr, true); return(ptr); }