4

我正在使用libsvm并且文档让我相信有一种方法可以输出输出分类准确性的可信概率。是这样吗?如果是这样,任何人都可以提供一个明确的例子来说明如何在代码中做到这一点?

目前,我正在以下列方式使用 Java 库

    SvmModel model = Svm.svm_train(problem, parameters);
    SvmNode x[] = getAnArrayOfSvmNodesForProblem();
    double predictedValue = Svm.svm_predict(model, x);
4

2 回答 2

8

鉴于您的代码片段,我将假设您想要使用与 libSVM 打包的 Java API 而不是jlibsvm提供的更详细的 API 。

要使用概率估计进行预测,请训练一个将svm_parameter 字段 概率 设置为 1的模型。然后,只需更改您的代码,使其调用svm 方法svm_predict_probability而不是svm_predict.

修改您的代码段,我们有:

parameters.probability = 1;
svm_model model = svm.svm_train(problem, parameters);

svm_node x[] = problem.x[0]; // let's try the first data pt in problem
double[] prob_estimates = new double[NUM_LABEL_CLASSES]; 
svm.svm_predict_probability(model, x, prob_estimates);

值得知道的是,使用多类概率估计进行训练可以改变分类器所做的预测。有关这方面的更多信息,请参阅Calculating Nearest Match to Mean/Stddev Pair With LibSVM问题。

于 2010-05-04T01:22:39.217 回答
1

接受的答案就像一个魅力。确保probability = 1在训练期间进行设置。

如果您在置信度未达到阈值时尝试放弃预测,以下是代码示例:

double confidenceScores[] = new double[model.nr_class];
svm.svm_predict_probability(model, svmVector, confidenceScores);

/*System.out.println("text="+ text);
for (int i = 0; i < model.nr_class; i++) {
    System.out.println("i=" + i + ", labelNum:" + model.label[i] + ", name=" + classLoadMap.get(model.label[i]) + ", score="+confidenceScores[i]);
}*/

//finding max confidence; 
int maxConfidenceIndex = 0;
double maxConfidence = confidenceScores[maxConfidenceIndex];
for (int i = 1; i < confidenceScores.length; i++) {
    if(confidenceScores[i] > maxConfidence){
        maxConfidenceIndex = i;
        maxConfidence = confidenceScores[i];
    }
}

double threshold = 0.3; // set this based data & no. of classes
int labelNum = model.label[maxConfidenceIndex];
// reverse map number to name
String targetClassLabel = classLoadMap.get(labelNum); 
LOG.info("classNumber:{}, className:{}; confidence:{}; for text:{}",
        labelNum, targetClassLabel, (maxConfidence), text);
if (maxConfidence < threshold ) {
    LOG.info("Not enough confidence; threshold={}", threshold);
    targetClassLabel = null;
}
return targetClassLabel;
于 2015-02-03T07:54:03.043 回答