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