-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnbc.pl
147 lines (126 loc) · 2.85 KB
/
nbc.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/perl
use strict;
use warnings;
use List::Util qw(max);
#Stephen Johnson
#sej917 // 11065472
#Assignment 1 for CMPT898
sub logsumexp{
#my $id_max = 0;
my $max = max(@_);
#print "MAX: $max\n";
my $exp = 0;
foreach my $var(@_){
$exp += exp($var - $max);
}
my $trick = $max + log($exp);
return $trick;
}
sub argmax{
my $id_max = 0;
my $max = max(@_);
for (my $i = 0; $i <= $#_; $i++){
if ($_[$i] == $max){
$id_max = $i;
}
}
return $id_max;
}
#File I/O
open(MYFILE2, ">nbc_summary.txt");
my $file = $ARGV[0];
my $num_tests = `wc -l < $file`;
for (my $n = 0; $n < $num_tests; $n++){
open(MYFILE, $file);
my %array;
my $row = 0; #number of examples
my $col = 0;
my $num_feat = 0;
my @test;
my $test_class;
while(<MYFILE>){
chomp($_);
if ($row == 0){
$num_feat++ while ($_ =~ /\t/g);
}
if($row != $n){
for (my $j = 0; $j < $num_feat; $j++){
$array{$row}{$j} = (split)[$j];
}
}else{
for (my $j = 0; $j < $num_feat; $j++){
$test[$j] = (split)[$j];
}
}
$row++;
}
close(MYFILE);
$test_class = $test[0];
my @N_c; # number of examples in class c
my %N_jc; # number of examples of feature j in class c
my $possible_values = 5; #number of possible values for a feature
for (my $i = 0; $i < $row; $i++){
for (my $j = 1; $j < $num_feat; $j++){
for( my $k = 0; $k < $possible_values; $k++){
$N_jc{$i}{$j}{$k} = 0;
}
}
}
my @pi_c;
my %theta_jc;
for(my $i= 0; $i < $row; $i++){
if($i != $n){
my $c = $array{$i}{0}; # Class label of ith example;
$N_c[$c] += 1 ;
for(my $j = 1; $j < $num_feat; $j++){
$N_jc{$c}{$j}{$array{$i}{$j}} += 1;
}
}
}
my @a_c;
my $a_o;
for (my $i = 0; $i <= $#N_c; $i++){
$a_c[$i] = 1;#$N_c[$i]/$row;
$a_o += $a_c[$i];
}
my $b_0 = 1;
my $b_1 = 1;
#this works! for MLE estimates
for(my $i = 0; $i <= $#N_c; $i++){
$pi_c[$i] = ($N_c[$i] + $a_c[$i]) / ($row + $a_o); #pi_c = Nc / N
}
for (my $c = 0; $c <= $#N_c; $c++){
for (my $j = 1; $j < $num_feat; $j++){
for (my $k = 0; $k < $possible_values; $k++){
$theta_jc{$c}{$j}{$k} = ($N_jc{$c}{$j}{$k} + $b_0)/ ($N_c[$c] + $b_0 + $b_1); #theta_jc = Njc / Nc
}
}
}
#MAP estimates
#my $pi_c = ($N_c + $a_c[$c]) / ($N + $a_o);
#my $theta_jc = ($N_jc + $b_1 ) / ($N_c + $b_0 + $b_1 );
#PREDICTION STEP
my @L_ic;
for (my $c = 0; $c <= $#N_c; $c++){
$L_ic[$c] = 0;
}
my @p_ic;
for (my $c = 0; $c <= $#N_c; $c++){
$L_ic[$c] = log($pi_c[$c]);
for (my $j = 1; $j < $num_feat; $j++){
if ($theta_jc{$c}{$j}{$test[$j]} == 0){
$L_ic[$c] += log(1);
}else{
$L_ic[$c] += log($theta_jc{$c}{$j}{$test[$j]});
}
}
}
for (my $c = 0; $c <= $#N_c; $c++){
$p_ic[$c] = exp($L_ic[$c] - &logsumexp(@L_ic));
}
my $y_i = &argmax(@p_ic);
if ($y_i != $test_class){
print "ERROR: Test $n failed, Predicted class: $y_i Actual class: $test_class\n";
}
print MYFILE2 "$y_i\n";
}