/// <summary> /// Predicts the class memberships of all the vectors in the problem. /// </summary> /// <param name="problem">The SVM Problem to solve</param> /// <param name="model">The Model to use</param> /// <param name="predictProbability">Whether to output a distribution over the classes</param> /// <returns>Percentage correctly labelled</returns> public static PredictionResult Predict( Problem problem, Model model, bool predictProbability) { int correct = 0; PredictionResult result = new PredictionResult(model.Parameter); SvmType svmType = Procedures.SvmGetSvmType(model); int numberOfClasses = Procedures.SvmGetNrClass(model); int[] labels = new int[numberOfClasses]; double[] probEstimates = null; if (predictProbability) { if (svmType == SvmType.EPSILON_SVR || svmType == SvmType.NU_SVR) { log.Info("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=" + Procedures.SvmGetSvrProbability(model)); } else { probEstimates = new double[numberOfClasses]; } } Procedures.SvmGetLabels(model, labels); for (int j = 0; j < numberOfClasses; j++) { result.AddLabel(labels[j]); } for (int i = 0; i < problem.Count; i++) { ClassificationClass item = new ClassificationClass(); item.Target = problem.Y[i]; Node[] xValues = problem.X[i]; if (predictProbability && (svmType == SvmType.C_SVC || svmType == SvmType.NU_SVC)) { Procedures.SvmPredictProbability(model, xValues, probEstimates); double max = 0; for (int j = 0; j < numberOfClasses; j++) { if (probEstimates[j] > max) { max = probEstimates[j]; item.Actual = labels[j]; } item.Add(probEstimates[j]); } } else { item.Actual = (int)Procedures.SvmPredict(model, xValues); } if (item.Actual == item.Target) { correct++; } result.Set(item); } result.CorrectProbability = (double)correct / problem.Count; return(result); }
/// <summary> /// Constructor. /// </summary> /// <param name="model">Model to evaluate</param> /// <param name="problem">Problem to evaluate</param> /// <param name="category">Category to evaluate for</param> public PerformanceEvaluator(Model model, Problem problem, double category) { result = Prediction.Predict(problem, model, true); ParseResults(problem.Y, category); computeStatistics(); }