Esempio n. 1
0
 /// <summary>
 /// Predict the probability distributions over all labels for each data point in a problem.
 /// </summary>
 /// <param name="model">The model to use</param>
 /// <param name="problem">The problem to solve</param>
 /// <returns>A distribution over labels for each data point</returns>
 public static double[][] PredictLabelsProbability(this Model model, Problem problem)
 {
     return(problem.X.Select(o => model.PredictProbability(o)).ToArray());
 }
Esempio n. 2
0
        /// <summary>
        /// Predicts the class memberships of all the vectors in the problem.
        /// </summary>
        /// <param name="problem">The SVM Problem to solve</param>
        /// <param name="outputFile">File for result output</param>
        /// <param name="model">The Model to use</param>
        /// <param name="predict_probability">Whether to output a distribution over the classes</param>
        /// <returns>Percentage correctly labelled</returns>
        public static double Predict(
            Problem problem,
            string outputFile,
            Model model,
            bool predict_probability)
        {
            int          correct = 0;
            int          total = 0;
            double       error = 0;
            double       sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
            StreamWriter output = outputFile != null ? new StreamWriter(outputFile) : null;

            SvmType svm_type = Procedures.svm_get_svm_type(model);
            int     nr_class = Procedures.svm_get_nr_class(model);

            int[]    labels         = new int[nr_class];
            double[] prob_estimates = null;

            if (predict_probability)
            {
                if (svm_type == SvmType.EPSILON_SVR || svm_type == SvmType.NU_SVR)
                {
                    Console.WriteLine("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=" + Procedures.svm_get_svr_probability(model));
                }
                else
                {
                    Procedures.svm_get_labels(model, labels);
                    prob_estimates = new double[nr_class];
                    if (output != null)
                    {
                        output.Write("labels");
                        for (int j = 0; j < nr_class; j++)
                        {
                            output.Write(" " + labels[j]);
                        }
                        output.Write("\n");
                    }
                }
            }
            for (int i = 0; i < problem.Count; i++)
            {
                double target = problem.Y[i];
                Node[] x      = problem.X[i];

                double v;
                if (predict_probability && (svm_type == SvmType.C_SVC || svm_type == SvmType.NU_SVC))
                {
                    v = Procedures.svm_predict_probability(model, x, prob_estimates);
                    if (output != null)
                    {
                        output.Write(v + " ");
                        for (int j = 0; j < nr_class; j++)
                        {
                            output.Write(prob_estimates[j] + " ");
                        }
                        output.Write("\n");
                    }
                }
                else
                {
                    v = Procedures.svm_predict(model, x);
                    if (output != null)
                    {
                        output.Write(v + "\n");
                    }
                }

                if (v == target)
                {
                    ++correct;
                }
                error += (v - target) * (v - target);
                sumv  += v;
                sumy  += target;
                sumvv += v * v;
                sumyy += target * target;
                sumvy += v * target;
                ++total;
            }
            if (output != null)
            {
                output.Close();
            }

            if (model.Parameter.SvmType == SvmType.EPSILON_SVR || model.Parameter.SvmType == SvmType.NU_SVR)
            {
                return(error / total);
            }
            else
            {
                return((double)correct / total);
            }
        }
 /// <summary>
 /// Determines the Range transform for the provided problem.  Uses the default lower and upper bounds.
 /// </summary>
 /// <param name="prob">The Problem to analyze</param>
 /// <returns>The Range transform for the problem</returns>
 public static RangeTransform Compute(Problem prob)
 {
     return(Compute(prob, DEFAULT_LOWER_BOUND, DEFAULT_UPPER_BOUND));
 }