首页 > 解决方案 > PHP TNTClassifier似然概率分布

问题描述

我正在使用 TNT 搜索文本分类模块 https://github.com/teamtnt/tntsearch,它运行良好,问题是我不知道如何解释结果 - 更具体地说是正确匹配的可能性。我读过它使用朴素贝叶斯分类器,但我无法找到结果是哪种概率分布。我有自己的大约 50 个值(50 / 10 = 5 个类别)的小型测试数据集,并且猜测相当正确。

但是,此工具提供的似然数是大约 -15 到 -25 范围内的负数。

问题是,什么值可以被解释为不可信?假设该工具只有 <33% 的把握。这个假设对应的值是多少?

标签: phpclassificationprobability

解决方案


我已经与 TNTSearch 开发人员取得了联系。分类器实际上并没有返回一个概率,而是一个“最高分”。并且只为最好的匹配。

按照建议,我对代码进行了一些更改。

在课堂TeamTNT\TNTSearch\Classifier\TNTClassifier上,我更改了predict方法中的位(softmax 函数灵感来自这里):

public function predict($statement)
{
    $words = $this->tokenizer->tokenize($statement);

    $best_likelihoods = [];
    $best_likelihood = -INF;
    $best_type       = '';
    foreach ($this->types as $type) {
        $best_likelihoods[$type] = -INF;
        $likelihood = log($this->pTotal($type)); // calculate P(Type)
        $p          = 0;
        foreach ($words as $word) {
            $word = $this->stemmer->stem($word);
            $p += log($this->p($word, $type));
        }
        $likelihood += $p; // calculate P(word, Type)
        if ($likelihood > $best_likelihood) {
            $best_likelihood = $likelihood;
            $best_likelihoods[$type] = $likelihood;
            $best_type       = $type;
        }
    }

    return [
        'likelihood' => $best_likelihood,
        'likelihoods' => $best_likelihoods,
        'probability' => $this->softmax($best_likelihoods),
        'label'      => $best_type
    ];
}

然后可以在 中找到百分比概率$guess['probability']['$label']


推荐阅读