| 1 | package AI::ConfusionMatrix; | ||
| 2 | $AI::ConfusionMatrix::VERSION = '0.003'; | ||
| 3 | use Carp; | ||
| 4 | use Exporter 'import'; | ||
| 5 | our @EXPORT= qw (makeConfusionMatrix); | ||
| 6 | use strict; | ||
| 7 | use Tie::File; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | # ABSTRACT: Make a confusion matrix | ||
| 11 | |||
| 12 | sub makeConfusionMatrix { | ||
| 13 | my ($matrix, $filename) = @_; | ||
| 14 | carp ('First argument must be a hash reference') if ref($matrix) ne 'HASH'; | ||
| 15 | tie my @array, 'Tie::File', $filename or carp "$!"; | ||
| 16 | my $n = 1; | ||
| 17 | my @columns; | ||
| 18 | my @expected = sort keys %{$matrix}; | ||
| 19 | my %stats; | ||
| 20 | my %totals; | ||
| 21 | for my $expected (@expected) { | ||
| 22 | $array[$n] = $expected; | ||
| 23 | ++$n; | ||
| 24 | $stats{$expected}{'fn'} = 0; | ||
| 25 | $stats{$expected}{'tp'} = 0; | ||
| 26 | |||
| 27 | for my $predicted (keys %{$matrix->{$expected}}) { | ||
| 28 | # Ensure that the False Positive counter is defined to be able to compute the total later | ||
| 29 | $stats{$predicted}{'fp'} //= 0; | ||
| 30 | $stats{$expected}{'total'} += $matrix->{$expected}->{$predicted}; | ||
| 31 | $stats{$expected}{'tp'} += $matrix->{$expected}->{$predicted} if $expected == $predicted; | ||
| 32 | if ($expected != $predicted) { | ||
| 33 | $stats{$expected}{'fn'} += $matrix->{$expected}->{$predicted}; | ||
| 34 | $stats{$predicted}{'fp'} += $matrix->{$expected}->{$predicted}; | ||
| 35 | } | ||
| 36 | $totals{$predicted} += $matrix->{$expected}->{$predicted}; | ||
| 37 | # Add the label to the array of columns if it does not contain it already | ||
| 38 | push @columns, $predicted unless _findIndex($predicted, \@columns); | ||
| 39 | } | ||
| 40 | |||
| 41 | $stats{$expected}{'acc'} = sprintf("%.2f", ($stats{$expected}{'tp'} * 100) / $stats{$expected}{'total'}); | ||
| 42 | } | ||
| 43 | |||
| 44 | for my $expected (@expected) { | ||
| 45 | $totals{'total'} += $stats{$expected}{'total'}; | ||
| 46 | $totals{'tp'} += $stats{$expected}{'tp'}; | ||
| 47 | $totals{'fn'} += $stats{$expected}{'fn'}; | ||
| 48 | $totals{'fp'} += $stats{$expected}{'fp'}; | ||
| 49 | $totals{'acc'} += $stats{$expected}{'acc'}; | ||
| 50 | $stats{$expected}{'acc'} .= '%'; | ||
| 51 | } | ||
| 52 | |||
| 53 | $totals{'acc'} = sprintf("%.2f%%", $totals{'acc'} / @expected); | ||
| 54 | @columns = sort @columns; | ||
| 55 | map {$array[0] .= ',' . $_} join ',', (@columns, 'TOTAL', 'TP', 'FP', 'FN', 'ACC'); | ||
| 56 | $n = 1; | ||
| 57 | for my $expected (@expected) { | ||
| 58 | my $lastIndex = 0; | ||
| 59 | my $index; | ||
| 60 | for my $predicted (sort keys %{$matrix->{$expected}}) { | ||
| 61 | # Calculate the index of the label in the array of columns | ||
| 62 | $index = _findIndex($predicted, \@columns); | ||
| 63 | # Print some commas to get to the column of the next value predicted | ||
| 64 | $array[$n] .= ',' x ($index - $lastIndex) . %{$matrix}{$expected}->{$predicted}; | ||
| 65 | $lastIndex = $index; | ||
| 66 | } | ||
| 67 | |||
| 68 | # Get to the columns of the stats | ||
| 69 | $array[$n] .= ',' x (scalar(@columns) - $lastIndex + 1); | ||
| 70 | $array[$n] .= join ',', ( | ||
| 71 | $stats{$expected}{'total'}, | ||
| 72 | $stats{$expected}{'tp'}, | ||
| 73 | $stats{$expected}{'fp'}, | ||
| 74 | $stats{$expected}{'fn'}, | ||
| 75 | $stats{$expected}{'acc'} | ||
| 76 | ); | ||
| 77 | ++$n; | ||
| 78 | } | ||
| 79 | # Print the TOTAL row to the csv file | ||
| 80 | $array[$n] = 'TOTAL,'; | ||
| 81 | map {$array[$n] .= $totals{$_}. ','} (sort keys %totals)[0 .. $#columns]; | ||
| 82 | $array[$n] .= join ',', ($totals{'total'}, $totals{'tp'}, $totals{'fp'}, $totals{'fn'}, $totals{'acc'}) | ||
| 83 | } | ||
| 84 | |||
| 85 | sub _findIndex { | ||
| 86 | my ($string, $array) = @_; | ||
| 87 | for (0 .. @$array - 1) { | ||
| 88 | return $_ + 1 if ($string eq @{$array}[$_]); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | =head1 NAME | ||
| 93 | |||
| 94 | AI::ConfusionMatrix - make a confusion matrix | ||
| 95 | |||
| 96 | =head1 SYNOPSIS | ||
| 97 | |||
| 98 | my %matrix; | ||
| 99 | |||
| 100 | Loop over your tests | ||
| 101 | |||
| 102 | --- | ||
| 103 | |||
| 104 | $matrix{$expected}{$predicted} += 1; | ||
| 105 | |||
| 106 | --- | ||
| 107 | |||
| 108 | makeConfusionMatrix(\%matrix, 'output.csv'); | ||
| 109 | |||
| 110 | |||
| 111 | =head1 DESCRIPTION | ||
| 112 | |||
| 113 | This module prints a L<confusion matrix|https://en.wikipedia.org/wiki/Confusion_matrix> from a hash reference. This module tries to be generic enough to be used within a lot of machine learning projects. | ||
| 114 | |||
| 115 | =head3 Function | ||
| 116 | |||
| 117 | =head4 C<makeConfusionMatrix($hash_ref, $filename)> | ||
| 118 | |||
| 119 | This function makes a confusion matrix from C<$hash_ref> and writes it to C<$filename>. | ||
| 120 | |||
| 121 | Example: | ||
| 122 | |||
| 123 | makeConfusionMatrix(\%matrix, 'output.csv'); | ||
| 124 | |||
| 125 | The hash reference must look like this : | ||
| 126 | |||
| 127 | $VAR1 = { | ||
| 128 | |||
| 129 | |||
| 130 | 'value_expected1' => { | ||
| 131 | 'value_predicted1' => value | ||
| 132 | }, | ||
| 133 | 'value_expected2' => { | ||
| 134 | 'value_predicted1' => value, | ||
| 135 | 'value_predicted2' => value | ||
| 136 | }, | ||
| 137 | 'value_expected3' => { | ||
| 138 | 'value_predicted3' => value | ||
| 139 | } | ||
| 140 | |||
| 141 | }; | ||
| 142 | |||
| 143 | The output will be in CSV. Here is an example: | ||
| 144 | |||
| 145 | |||
| 146 | ,1997,1998,2001,2003,2005,2008,2012,2015,TOTAL,TP,FP,FN,ACC | ||
| 147 | 1997,1,,,,,,,1,2,1,0,1,50.00% | ||
| 148 | 1998,,1,,,,,,,1,1,0,0,100.00% | ||
| 149 | 2001,,,1,,,,,,1,1,0,0,100.00% | ||
| 150 | 2003,,,,5,,,,2,7,5,0,2,71.43% | ||
| 151 | 2005,,,,,5,,,4,9,5,0,4,55.56% | ||
| 152 | 2008,,,,,,3,,,3,3,0,0,100.00% | ||
| 153 | 2012,,,,,,,5,,5,5,0,0,100.00% | ||
| 154 | 2015,,,,,,,,2,2,2,7,0,100.00% | ||
| 155 | TOTAL,1,1,1,5,5,3,5,9,30,23,7,7,84.62% | ||
| 156 | |||
| 157 | Prettified: | ||
| 158 | |||
| 159 | |||
| 160 | | | 1997 | 1998 | 2001 | 2003 | 2005 | 2008 | 2012 | 2015 | TOTAL | TP | FP | FN | ACC | | ||
| 161 | |-------|------|------|------|------|------|------|------|------|-------|----|----|----|---------| | ||
| 162 | | 1997 | 1 | | | | | | | 1 | 2 | 1 | 0 | 1 | 50.00% | | ||
| 163 | | 1998 | | 1 | | | | | | | 1 | 1 | 0 | 0 | 100.00% | | ||
| 164 | | 2001 | | | 1 | | | | | | 1 | 1 | 0 | 0 | 100.00% | | ||
| 165 | | 2003 | | | | 5 | | | | 2 | 7 | 5 | 0 | 2 | 71.43% | | ||
| 166 | | 2005 | | | | | 5 | | | 4 | 9 | 5 | 0 | 4 | 55.56% | | ||
| 167 | | 2008 | | | | | | 3 | | | 3 | 3 | 0 | 0 | 100.00% | | ||
| 168 | | 2012 | | | | | | | 5 | | 5 | 5 | 0 | 0 | 100.00% | | ||
| 169 | | 2015 | | | | | | | | 2 | 2 | 2 | 7 | 0 | 100.00% | | ||
| 170 | | TOTAL | 1 | 1 | 1 | 5 | 5 | 3 | 5 | 9 | 30 | 23 | 7 | 7 | 84.62% | | ||
| 171 | |||
| 172 | =over | ||
| 173 | |||
| 174 | =item TP: | ||
| 175 | |||
| 176 | True Positive | ||
| 177 | |||
| 178 | =item FP: | ||
| 179 | |||
| 180 | False Positive | ||
| 181 | |||
| 182 | =item FN: | ||
| 183 | |||
| 184 | False Negative | ||
| 185 | |||
| 186 | =item ACC: | ||
| 187 | |||
| 188 | Accuracy | ||
| 189 | |||
| 190 | =back | ||
| 191 | |||
| 192 | =head1 AUTHOR | ||
| 193 | |||
| 194 | Vincent Lequertier <sky@riseup.net> | ||
| 195 | |||
| 196 | =head1 LICENSE | ||
| 197 | |||
| 198 | This library is free software; you can redistribute it and/or modify | ||
| 199 | it under the same terms as Perl itself. | ||
| 200 | |||
| 201 | =cut | ||
| 202 | |||
| 203 | 1; | ||
| 204 | |||
| 205 | # vim: set ts=4 sw=4 tw=0 fdm=marker : | ||
| 206 |