blib/lib/AI/ConfusionMatrix.pm untested

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 5 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 92 CC 6 SCAR 28.2
CC
6
Cov
0%
makeConfusionMatrix
30.0
_findIndex
6.0
BEGIN
2.0
CRAP 16.8
1 package AI::ConfusionMatrix;
2 $AI::ConfusionMatrix::VERSION = '0.002';
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 my %totals;
19 for my $expected (@expected) {
20 $array[$n] = $expected;
21 ++$n;
22 $stats{$expected}{'fn'} = 0;
23 $stats{$expected}{'tp'} = 0;
24  
25 for my $predicted (keys %{$matrix->{$expected}}) {
26 $stats{$expected}{'total'} += $matrix->{$expected}->{$predicted};
27 $stats{$expected}{'tp'} += $matrix->{$expected}->{$predicted} if $expected == $predicted;
28 if ($expected != $predicted) {
29 $stats{$expected}{'fn'} += $matrix->{$expected}->{$predicted};
30 $stats{$predicted}{'fp'} += $matrix->{$expected}->{$predicted};
31 }
32 $totals{$predicted} += $matrix->{$expected}->{$predicted};
33 push @columns, $predicted unless _findIndex($predicted, \@columns);
34 }
35  
36 $stats{$expected}{'acc'} = sprintf("%.2f%", ($stats{$expected}{'tp'} * 100) / $stats{$expected}{'total'});
37 }
38  
39 for my $expected (@expected) {
40 $totals{'total'} += $stats{$expected}{'total'};
41 $totals{'tp'} += $stats{$expected}{'tp'};
42 $totals{'fn'} += $stats{$expected}{'fn'};
43 $totals{'fp'} += $stats{$expected}{'fp'};
44 $totals{'acc'} += $stats{$expected}{'acc'};
45 }
46  
47 $totals{'acc'} = sprintf("%.2f%", $totals{'acc'} / @expected);
48 @columns = sort @columns;
49 map {$array[0] .= ',' . $_} join ',', (@columns, 'TOTAL', 'TP', 'FP', 'FN', 'ACC');
50 $n = 1;
51 for my $expected (@expected) {
52 $stats{$expected}{'fp'} //= 0;
53 my $lastIndex = 0;
54 for my $predicted (sort keys %{$matrix->{$expected}}) {
55 $index = _findIndex($predicted, \@columns);
56 $array[$n] .= ',' x ($index - $lastIndex) . %{$matrix}{$expected}->{$predicted};
57 $lastIndex = $index;
58 }
59  
60 $array[$n] .= ',' x (scalar(@columns) - $lastIndex + 1);
61 $array[$n] .= join ',', (
62 $stats{$expected}{'total'},
63 $stats{$expected}{'tp'},
64 $stats{$expected}{'fp'},
65 $stats{$expected}{'fn'},
66 $stats{$expected}{'acc'}
67 );
68 ++$n;
69 }
70 $array[$n] = 'TOTAL,';
71 map {$array[$n] .= $totals{$_}. ','} (sort keys %totals)[0 .. $#columns];
72 $array[$n] .= join ',', ($totals{'total'}, $totals{'tp'}, $totals{'fp'}, $totals{'fn'}, $totals{'acc'})
73 }
74  
75 sub _findIndex {
76 my ($string, $array) = @_;
77 for (0 .. scalar(@{$array})) {
78 return $_ + 1 if ($string eq @{$array}[$_]);
79 }
80 }
81  
82 =head1 NAME
83  
84 AI::ConfusionMatrix - make a confusion matrix
85  
86 =head1 SYNOPSIS
87  
88 my %matrix;
89  
90 Loop over your tests
91  
92 ---
93  
94 $matrix{$expected}{$predicted} += 1;
95  
96 ---
97  
98 makeConfusionMatrix(\%matrix, 'output.csv');
99  
100  
101 =head1 DESCRIPTION
102  
103 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.
104  
105 =head3 Function
106  
107 =head4 C<makeConfusionMatrix($hash_ref, $filename)>
108  
109 This function makes a confusion matrix from C<$hash_ref> and writes it to C<$filename>.
110  
111 Example:
112  
113 makeConfusionMatrix(\%matrix, 'output.csv');
114  
115 The hash reference must look like this :
116  
117 $VAR1 = {
118  
119  
120 'value_expected1' => {
121 'value_predicted1' => value
122 },
123 'value_expected2' => {
124 'value_predicted1' => value,
125 'value_predicted2' => value
126 },
127 'value_expected3' => {
128 'value_predicted3' => value
129 }
130  
131 };
132  
133 The output will be in CSV. Here is an example:
134  
135  
136 ,1997,1998,2001,2003,2005,2008,2012,2015,TOTAL,TP,FP,FN,ACC
137 1997,1,,,,,,,1,2,1,0,1,50.00%
138 1998,,1,,,,,,,1,1,0,0,100.00%
139 2001,,,1,,,,,,1,1,0,0,100.00%
140 2003,,,,5,,,,2,7,5,0,2,71.43%
141 2005,,,,,5,,,4,9,5,0,4,55.56%
142 2008,,,,,,3,,,3,3,0,0,100.00%
143 2012,,,,,,,5,,5,5,0,0,100.00%
144 2015,,,,,,,,2,2,2,7,0,100.00%
145 TOTAL,1,1,1,5,5,3,5,9,30,23,7,7,84.62%
146  
147 =over
148  
149 =item TP:
150  
151 True Positive
152  
153 =item FP:
154  
155 False Positive
156  
157 =item FN:
158  
159 False Negative
160  
161 =item ACC:
162  
163 Accuracy
164  
165 =back
166  
167 =head1 AUTHOR
168  
169 Vincent Lequertier <sky@riseup.net>
170  
171 =head1 LICENSE
172  
173 This library is free software; you can redistribute it and/or modify
174 it under the same terms as Perl itself.
175  
176 =cut
177  
178 1;
179  
180 # vim: set ts=4 sw=4 tw=0 fdm=marker :
181