-
Notifications
You must be signed in to change notification settings - Fork 5
/
rainbow-rank.pl
60 lines (46 loc) · 1.36 KB
/
rainbow-rank.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
#!/usr/bin/perl
# The above line is modified by ./Makefile to match the system's
# installed location for Perl.
# Script to process the output of rainbow program and produce
# statistics about the classification rankings
# Written by Andrew Ng <[email protected]>
#$nClasses = pop @ARGV;
#die "Specify number of classes on command line" if (!defined $nClasses);
$nClasses = -1;
$foo = <>;
die "Expected first line to be \#triannum" unless ($foo =~ /^\#/);
$nSamples = 0;
while (<>)
{
@arr = split(/ /);
if ($nClasses < 0) {
$nClasses = @arr - 3;
for ($i=1; $i <= $nClasses; $i++)
{ $rankCount[$i] = 0; }
}
$trueClass = $arr[1];
$rank = -100;
for ($i=2; $i <= $#arr; $i++)
{
($class, $ignore) = split(/:/, $arr[$i]);
undef $ignore;
if ($class eq $trueClass)
{ $rank = $i-1; }
}
die "Bad" if ($rank == -100);
$rankCount[$rank]++;
$nSamples++;
}
print "Rank counts: \n";
for ($i=1; $i <= $nClasses; $i++)
{ print $i, ":", $rankCount[$i], " "; }
print "\n";
$rankCountSum = 0;
$inverseRankSum = 0;
for ($i=1; $i <= $nClasses; $i++)
{
$inverseRankSum += (1.0 / $i) * $rankCount[$i];
$rankCountSum += $rankCount[$i];
print "With a rank of ", $i, " or better: ", $rankCountSum, "/", $nSamples, " (", $rankCountSum/$nSamples, ")\n";
}
print "average inverse rank (1.0 means all correct) is: ", $inverseRankSum/$nSamples, "\n";