line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Algorithm::NaiveBayes::Util; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
14
|
use strict; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
82
|
|
4
|
3
|
|
|
3
|
|
14
|
use base qw(Exporter); |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
221
|
|
5
|
3
|
|
|
3
|
|
18
|
use vars qw(@EXPORT_OK); |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
244
|
|
6
|
|
|
|
|
|
|
@EXPORT_OK = qw(sum sum_hash max variance add_hash rescale); |
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
13
|
use List::Util qw(max sum); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
1191
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub sum_hash { |
11
|
3
|
|
|
3
|
0
|
3
|
my $href = shift; |
12
|
3
|
|
|
|
|
32
|
return sum(values %$href); |
13
|
|
|
|
|
|
|
} |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub variance { |
16
|
0
|
|
|
0
|
0
|
0
|
my $array = shift; |
17
|
0
|
0
|
|
|
|
0
|
return 0 unless @$array > 1; |
18
|
0
|
0
|
|
|
|
0
|
my $mean = @_ ? shift : sum($array) / @$array; |
19
|
|
|
|
|
|
|
|
20
|
0
|
|
|
|
|
0
|
my $var = 0; |
21
|
0
|
|
|
|
|
0
|
$var += ($_ - $mean)**2 foreach @$array; |
22
|
0
|
|
|
|
|
0
|
return $var / (@$array - 1); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub add_hash { |
26
|
10
|
|
|
10
|
0
|
205
|
my ($first, $second) = @_; |
27
|
10
|
|
|
|
|
25
|
foreach my $k (keys %$second) { |
28
|
48
|
|
|
|
|
493
|
$first->{$k} += $second->{$k}; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub rescale { |
33
|
3
|
|
|
3
|
0
|
5
|
my ($scores) = @_; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Scale everything back to a reasonable area in logspace (near zero), un-loggify, and normalize |
36
|
3
|
|
|
|
|
4
|
my $total = 0; |
37
|
3
|
|
|
|
|
17
|
my $max = max(values %$scores); |
38
|
3
|
|
|
|
|
5
|
foreach (values %$scores) { |
39
|
6
|
|
|
|
|
32
|
$_ = exp($_ - $max); |
40
|
6
|
|
|
|
|
24
|
$total += $_**2; |
41
|
|
|
|
|
|
|
} |
42
|
3
|
|
|
|
|
10
|
$total = sqrt($total); |
43
|
3
|
|
|
|
|
6
|
foreach (values %$scores) { |
44
|
6
|
|
|
|
|
14
|
$_ /= $total; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
1; |