我正在使用 libsvm 文档使我相信有一种方法可以输出可信赖的输出分类准确性的可能性。是这样吗?如果是这样,谁能提供一个明确的示例,说明如何在代码中进行操作?

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

    SvmModel model = Svm.svm_train(problem, parameters);
    SvmNode x[] = getAnArrayOfSvmNodesForProblem();
    double predictedValue = Svm.svm_predict(model, x);
有帮助吗?

解决方案

鉴于您的代码刺,我将假设您要使用包装的Java API libsvm, ,而不是由 jlibsvm.

为了实现概率估计的预测,请训练一个模型 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);

值得知道具有多类概率估算的培训 可以改变预测 由分类器制造。有关此的更多信息,请参阅问题 与LIBSVM计算最接近的匹配/stddev对.

其他提示

接受的答案像魅力一样工作。确保设置 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;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top