File: rainbow-rank.pl

package info (click to toggle)
bow 19991122-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,544 kB
  • ctags: 2,987
  • sloc: ansic: 38,660; lisp: 1,072; makefile: 594; perl: 492; yacc: 149; sh: 91
file content (60 lines) | stat: -rw-r--r-- 1,389 bytes parent folder | download | duplicates (4)
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
#!/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 <ayn@ai.mit.edu>

#$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";