mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-11-24 22:07:33 +00:00
Prevent Division by zero error in classification report
This commit is contained in:
parent
b226a561cb
commit
bca2196b57
@ -113,6 +113,10 @@ class ClassificationReport
|
|||||||
{
|
{
|
||||||
foreach (['precision', 'recall', 'f1score'] as $metric) {
|
foreach (['precision', 'recall', 'f1score'] as $metric) {
|
||||||
$values = array_filter($this->$metric);
|
$values = array_filter($this->$metric);
|
||||||
|
if(0==count($values)) {
|
||||||
|
$this->average[$metric] = 0.0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
$this->average[$metric] = array_sum($values) / count($values);
|
$this->average[$metric] = array_sum($values) / count($values);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,4 +67,19 @@ class ClassificationReportTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->assertEquals([1 => 0.0, 2 => 1, 3 => 0], $report->getPrecision(), '', 0.01);
|
$this->assertEquals([1 => 0.0, 2 => 1, 3 => 0], $report->getPrecision(), '', 0.01);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testPreventDividedByZeroWhenPredictedLabelsAllNotMatch()
|
||||||
|
{
|
||||||
|
$labels = [1,2,3,4,5];
|
||||||
|
$predicted = [2,3,4,5,6];
|
||||||
|
|
||||||
|
$report = new ClassificationReport($labels, $predicted);
|
||||||
|
|
||||||
|
$this->assertEquals([
|
||||||
|
'precision' => 0,
|
||||||
|
'recall' => 0,
|
||||||
|
'f1score' => 0
|
||||||
|
], $report->getAverage(), '', 0.01);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user