Example #1
0
        /// <summary>
        /// Gives decision values on a test vector x given a model, and return the predicted label(classification) or the function value(regression).
        /// </summary>
        /// <param name="model"><see cref="Model"/>.</param>
        /// <param name="x">The test vector.</param>
        /// <param name="decisionValues">When this method returns, contains decision values if succeeded, or null if failed.</param>
        /// <returns>
        /// <para>For a classification model, the predicted class for x and decision values.</para>
        /// <para>For a regression model, <code>decisionValues[0]</code> and the returned value are both the function value of x calculated using the model. </para>
        /// <para>For a one-class model, <code>decisionValues[0]</code> is the decision value of x, while the returned value is +1/-1.</para>
        /// </returns>
        public static double PredictValues(Model model, NodeArray x, out double[] decisionValues)
        {
            decisionValues = null;

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

            unsafe
            {
                var m = model.NativePtr;
                switch (model.Parameter.SvmType)
                {
                case SvmType.CSVC:
                case SvmType.NuSVC:
                    var length = model.Classes * (model.Classes - 1) / 2;
                    decisionValues = new double[length];
                    return(NativeMethods.svm_predict_values(m, (NativeMethods.svm_node *)x.NativePtr, decisionValues));

                default:
                    decisionValues = new double[1];
                    return(NativeMethods.svm_predict_values(m, (NativeMethods.svm_node *)x.NativePtr, decisionValues));
                }
            }
        }
Example #2
0
        /// <summary>
        /// Does classification or regression on a test vector x given a model.
        /// </summary>
        /// <param name="model"><see cref="Model"/>.</param>
        /// <param name="x">The test vector.</param>
        /// <returns>
        /// <para>For a classification model, the predicted class for x is returned.</para>
        /// <para>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.</para>
        /// </returns>
        public static double Predict(Model model, NodeArray x)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            if (x == null)
            {
                throw new ArgumentNullException(nameof(x));
            }

            unsafe
            {
                var m = model.NativePtr;
                return(NativeMethods.svm_predict(m, (NativeMethods.svm_node *)x.NativePtr));
            }
        }
Example #3
0
        /// <summary>
        /// Does classification or regression on a test vector x given a model.
        /// </summary>
        /// <param name="model"><see cref="Model"/>.</param>
        /// <param name="x">The test vector.</param>
        /// <param name="probability">When this method returns, contains probability estimates if succeeded, or null if failed.</param>
        /// <returns>
        /// <para>For a classification model, the predicted class for x is returned.</para>
        /// <para>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.</para>
        /// </returns>
        /// <remarks>This methods returns valid probability when <see cref="Model.ProbabilityA"/> and <see cref="Model.ProbabilityB"/> are not null.</remarks>
        public static double Predict(Model model, NodeArray x, out double[] probability)
        {
            probability = null;

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

            unsafe
            {
                var m = model.NativePtr;
                probability = new double[model.Classes];
                return(NativeMethods.svm_predict_probability(m, (NativeMethods.svm_node *)x.NativePtr, probability));
            }
        }