0

这是我编写的一些代码,用于使用朴素贝叶斯分类器计算关于某些观察到的特征的标签概率。这旨在计算朴素贝叶斯公式而不进行平滑处理,并且旨在计算实际概率,因此请使用通常省略的分母。我遇到的问题是,对于示例(如下),“好”标签的概率> 1。(1.30612245)谁能帮我理解那是什么?这是天真的假设的副产品吗?

package NaiveBayes;

use Moose;

has class_counts => (is => 'ro', isa => 'HashRef[Int]', default => sub {{}});
has class_feature_counts => (is => 'ro', isa => 'HashRef[HashRef[HashRef[Num]]]', default => sub {{}});
has feature_counts => (is => 'ro', isa => 'HashRef[HashRef[Num]]', default => sub {{}});
has total_observations => (is => 'rw', isa => 'Num');

sub insert {
    my( $self, $class, $data ) = @_;
    $self->class_counts->{$class}++;
    $self->total_observations( ($self->total_observations||0) + 1 );
    for( keys %$data ){
        $self->feature_counts->{$_}->{$data->{$_}}++;
        $self->class_feature_counts->{$_}->{$class}->{$data->{$_}}++;
    }
    return $self;
}

sub classify {
    my( $self, $data ) = @_;
    my %probabilities;
    my $feature_probability = 1;
    for my $class( keys %{ $self->class_counts } ) {
        my $class_count = $self->class_counts->{$class};
        my $class_probability = $class_count / $self->total_observations;
        my($feature_probability, $conditional_probability) = (1) x 2;
        my( @feature_probabilities, @conditional_probabilities );
        for( keys %$data ){
            my $feature_count = $self->feature_counts->{$_}->{$data->{$_}};
            my $class_feature_count = $self->class_feature_counts->{$_}->{$class}->{$data->{$_}} || 0;
            next unless $feature_count;
            $feature_probability *= $feature_count / $self->total_observations;
            $conditional_probability *= $class_feature_count / $class_count;
        }
        $probabilities{$class} = $class_probability * $conditional_probability / $feature_probability;
     }
     return %probabilities;
}

__PACKAGE__->meta->make_immutable;
1;

例子:

#!/usr/bin/env perl

use Moose;
use NaiveBayes;

my $nb = NaiveBayes->new;

$nb->insert('good' , {browser => 'chrome'   ,host => 'yahoo'    ,country => 'us'});
$nb->insert('bad'  , {browser => 'chrome'   ,host => 'slashdot' ,country => 'us'});
$nb->insert('good' , {browser => 'chrome'   ,host => 'slashdot' ,country => 'uk'});
$nb->insert('good' , {browser => 'explorer' ,host => 'google'   ,country => 'us'});
$nb->insert('good' , {browser => 'explorer' ,host => 'slashdot' ,country => 'ca'});
$nb->insert('good' , {browser => 'opera'    ,host => 'google'   ,country => 'ca'});
$nb->insert('good' , {browser => 'firefox'  ,host => '4chan'    ,country => 'us'});
$nb->insert('good' , {browser => 'opera'    ,host => '4chan'    ,country => 'ca'});

my %classes = $nb->classify({browser => 'opera', host => '4chan', country =>'uk'});

my @classes = sort { $classes{$a} <=> $classes{$b} } keys %classes;

for( @classes ){
    printf( "%-20s : %5.8f\n", $_, $classes{$_} );
}

印刷:

bad                  : 0.00000000
good                 : 1.30612245

我不太担心 0 的概率,但更担心好的 > 1 的“概率”。我相信这是经典朴素贝叶斯定义的实现。

p(C│F_1 ...F_n )=(p(C)p(F_1 |C)...p(F_n |C))/(p(F_1)...p(F_n))

这怎么可能> 1?

4

1 回答 1

0

自从我正确使用 Perl 以来,我能够调试它已经太久了,但我想我可以看到问题出在哪里。特征向量 p(f_1 ... f_n) 的边际概率不是按照您看起来的方式计算的,这是作为具有单独参数的单独计算。相反,如果您有具有先验 p(c_1) 和 p(c_2) 的类 c_1 和 c_2,以及似然项 p(f | c_1) 和 p(f | c_2),那么 f 的边际概率是:

p(c_1)*p(f|c_1) + p(c_2)*p(f|c_2)

这就是为什么分母经常被删除的原因:它只涉及您已经使用的数量的总和。您想知道的有关相对概率的任何信息都可以计算为非归一化分数的比率,因此计算比例常数只有在您明确想要一个介于 0 和 1 之间的数字时才有用。

于 2012-12-20T10:59:09.743 回答