diff --git a/src/Phpml/Metric/Accuracy.php b/src/Phpml/Metric/Accuracy.php new file mode 100644 index 0000000..878cadd --- /dev/null +++ b/src/Phpml/Metric/Accuracy.php @@ -0,0 +1,39 @@ + $label) { + if($label===$predictedLabels[$index]) { + $score++; + } + } + + if($normalize) { + $score = $score / count($actualLabels); + } + + return $score; + } +} diff --git a/tests/Phpml/Metric/AccuracyTest.php b/tests/Phpml/Metric/AccuracyTest.php new file mode 100644 index 0000000..31bb0fc --- /dev/null +++ b/tests/Phpml/Metric/AccuracyTest.php @@ -0,0 +1,38 @@ +assertEquals(0.5, Accuracy::score($actualLabels, $predictedLabels)); + } + + public function testCalculateNotNormalizedScore() + { + $actualLabels = ['a', 'b', 'a', 'b']; + $predictedLabels = ['a', 'b', 'b', 'b']; + + $this->assertEquals(3, Accuracy::score($actualLabels, $predictedLabels, false)); + } + +}