1 | <?php |
||
9 | class ClassificationReport |
||
10 | { |
||
11 | public const MICRO_AVERAGE = 1; |
||
12 | |||
13 | public const MACRO_AVERAGE = 2; |
||
14 | |||
15 | public const WEIGHTED_AVERAGE = 3; |
||
16 | |||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | private $truePositive = []; |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | private $falsePositive = []; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | private $falseNegative = []; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | private $support = []; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | private $precision = []; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | private $recall = []; |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | private $f1score = []; |
||
51 | |||
52 | /** |
||
53 | * @var array |
||
54 | */ |
||
55 | private $average = []; |
||
56 | |||
57 | public function __construct(array $actualLabels, array $predictedLabels, int $average = self::MACRO_AVERAGE) |
||
68 | |||
69 | public function getPrecision(): array |
||
73 | |||
74 | public function getRecall(): array |
||
78 | |||
79 | public function getF1score(): array |
||
83 | |||
84 | public function getSupport(): array |
||
88 | |||
89 | public function getAverage(): array |
||
93 | |||
94 | private function aggregateClassificationResults(array $actualLabels, array $predictedLabels): void |
||
115 | |||
116 | private function computeMetrics(): void |
||
124 | |||
125 | private function computeAverage(int $average): void |
||
142 | |||
143 | private function computeMicroAverage(): void |
||
144 | { |
||
145 | $truePositive = array_sum($this->truePositive); |
||
146 | $falsePositive = array_sum($this->falsePositive); |
||
147 | $falseNegative = array_sum($this->falseNegative); |
||
148 | |||
149 | $precision = $this->computePrecision($truePositive, $falsePositive); |
||
150 | $recall = $this->computeRecall($truePositive, $falseNegative); |
||
151 | $f1score = $this->computeF1Score((float) $precision, (float) $recall); |
||
152 | |||
153 | $this->average = compact('precision', 'recall', 'f1score'); |
||
154 | } |
||
155 | |||
156 | private function computeMacroAverage(): void |
||
169 | |||
170 | private function computeWeightedAverage(): void |
||
188 | |||
189 | /** |
||
190 | * @return float|string |
||
191 | */ |
||
192 | private function computePrecision(int $truePositive, int $falsePositive) |
||
201 | |||
202 | /** |
||
203 | * @return float|string |
||
204 | */ |
||
205 | private function computeRecall(int $truePositive, int $falseNegative) |
||
214 | |||
215 | private function computeF1Score(float $precision, float $recall): float |
||
224 | |||
225 | private static function getLabelIndexedArray(array $actualLabels, array $predictedLabels): array |
||
233 | } |
||
234 |