| 1 | #!/usr/local/bin/perl | ||
| 2 | # -*-Perl-*- | ||
| 3 | # weather.perl -- Tom Fawcett Tue Jan 2 1996 | ||
| 4 | # | ||
| 5 | # Description: | ||
| 6 | # | ||
| 7 | # Simple test/demo program for Statistics/LTU.pm. | ||
| 8 | # | ||
| 9 | # Example set is a very simple illustration domain taken from | ||
| 10 | # Quinlan's article on decision trees: | ||
| 11 | # | ||
| 12 | # @ARTICLE{Quinlan86, | ||
| 13 | # author = "J.R. Quinlan", | ||
| 14 | # year = "1986", | ||
| 15 | # title = "Induction of Decision Trees", | ||
| 16 | # journal = "Machine Learning", | ||
| 17 | # volume = "1", | ||
| 18 | # pages = "81--106", | ||
| 19 | # publisher = "Kluwer Academic Publishers, Boston" | ||
| 20 | # } | ||
| 21 | |||
| 22 | |||
| 23 | # | ||
| 24 | require 5; | ||
| 25 | use Statistics::LTU; | ||
| 26 | |||
| 27 | print "LTU version is $Statistics::LTU::VERSION\n"; | ||
| 28 | print "\$LTU_PLUS = $LTU_PLUS, \$LTU_MINUS = $LTU_MINUS\n"; | ||
| 29 | |||
| 30 | # Individual features to be used | ||
| 31 | @Features = ("sunny", "overcast", "rain",# clouds | ||
| 32 | "hot", "mild", "cool", # temperature | ||
| 33 | "humid", "normal", "dry", # humidity | ||
| 34 | "windy", "calm" # wind | ||
| 35 | ); | ||
| 36 | |||
| 37 | $N_FEATURES = $#Features + 1; | ||
| 38 | |||
| 39 | # The raw examples, taken from Quinlan's paper. | ||
| 40 | # Note that for simplicity we just represent the features | ||
| 41 | # as a string. | ||
| 42 | @Quinlan_Examples = ( | ||
| 43 | ["sunny hot humid calm", "n"], | ||
| 44 | ["sunny hot humid windy","n"], | ||
| 45 | ["overcast hot humid calm", "y"], | ||
| 46 | ["rain mild humid calm", "y"], | ||
| 47 | ["rain cool normal calm", "y"], | ||
| 48 | ["rain cool normal windy","n"], | ||
| 49 | ["overcast cool normal windy","y"], | ||
| 50 | ["sunny mild humid calm", "n"], | ||
| 51 | ["sunny cool normal calm", "y"], | ||
| 52 | ["rain mild normal calm", "y"], | ||
| 53 | ["sunny mild normal windy","y"], | ||
| 54 | ["overcast mild humid windy","y"], | ||
| 55 | ["overcast hot normal calm", "y"], | ||
| 56 | ["rain mild humid windy","y"] | ||
| 57 | ); | ||
| 58 | |||
| 59 | |||
| 60 | # Create the example set. Format of @Examples is | ||
| 61 | # ( [[...feature vector...], class], [[...feature vector...], class], ...) | ||
| 62 | # | ||
| 63 | |||
| 64 | @Examples = (); | ||
| 65 | |||
| 66 | foreach $example (@Quinlan_Examples) { | ||
| 67 | ($feature_string, $class) = @{$example}; | ||
| 68 | |||
| 69 | @Values = (0) x $N_FEATURES; | ||
| 70 | for $i (0 .. $#Features) { | ||
| 71 | $feature = $Features[$i]; | ||
| 72 | $Values[$i] = 1 if $feature_string =~ /$feature/i; | ||
| 73 | } | ||
| 74 | |||
| 75 | push(@Examples, [\@Values, | ||
| 76 | ($class eq "y" ? $LTU_PLUS : $LTU_MINUS)] | ||
| 77 | ); | ||
| 78 | } | ||
| 79 | |||
| 80 | # Create the LTU. Enable automatic feature scaling. | ||
| 81 | $ltu = new Statistics::LTU::ACR($N_FEATURES, 1); | ||
| 82 | |||
| 83 | # This is the main loop that trains and tests the LTU. | ||
| 84 | |||
| 85 | for $iter (1 .. 10) { | ||
| 86 | |||
| 87 | # Train the LTU | ||
| 88 | for $example (@Examples) { | ||
| 89 | ($features_ref, $class) = @{$example}; | ||
| 90 | $ltu->train($features_ref, $class); | ||
| 91 | } | ||
| 92 | |||
| 93 | # Test the LTU. We really don't need to do this separately from | ||
| 94 | # the eval_on_set since we could figure out accuracy from the | ||
| 95 | # stats eval_on_set returns. | ||
| 96 | # | ||
| 97 | $correct = 0; | ||
| 98 | for $example (@Examples) { | ||
| 99 | ($features_ref, $class) = @{$example}; | ||
| 100 | if ($ltu->correctly_classifies($features_ref, $class)) { | ||
| 101 | $correct++; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | ($TN, $FP, $FN, $TP) = $ltu->eval_on_set(\@Examples); | ||
| 106 | |||
| 107 | print "\nIteration $iter. LTU accuracy is "; | ||
| 108 | print $correct / ($#Examples + 1), "\n"; | ||
| 109 | print "True negs=$TN, False pos=$FP, False negs=$FN, True pos=$TP\n"; | ||
| 110 | |||
| 111 | $ltu->print; | ||
| 112 | } | ||
| 113 | |||
| 114 | ##### End of weather.perl |