From 12b8b118dd8f9d5805a27823c0e1d2630395ba97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Monlla=C3=B3?= Date: Mon, 24 Apr 2017 17:47:30 +0800 Subject: [PATCH] Fix division by 0 error during normalization (#83) * Fix division by 0 error during normalization std is 0 when a feature has the same value in samples. * Expand std normalization test --- src/Phpml/Preprocessing/Normalizer.php | 7 ++++++- tests/Phpml/Preprocessing/NormalizerTest.php | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Phpml/Preprocessing/Normalizer.php b/src/Phpml/Preprocessing/Normalizer.php index 42a8f1c..8392db7 100644 --- a/src/Phpml/Preprocessing/Normalizer.php +++ b/src/Phpml/Preprocessing/Normalizer.php @@ -134,7 +134,12 @@ class Normalizer implements Preprocessor private function normalizeSTD(array &$sample) { foreach ($sample as $i => $val) { - $sample[$i] = ($sample[$i] - $this->mean[$i]) / $this->std[$i]; + if ($this->std[$i] != 0) { + $sample[$i] = ($sample[$i] - $this->mean[$i]) / $this->std[$i]; + } else { + // Same value for all samples. + $sample[$i] = 0; + } } } } diff --git a/tests/Phpml/Preprocessing/NormalizerTest.php b/tests/Phpml/Preprocessing/NormalizerTest.php index 07d121c..2492fae 100644 --- a/tests/Phpml/Preprocessing/NormalizerTest.php +++ b/tests/Phpml/Preprocessing/NormalizerTest.php @@ -111,6 +111,9 @@ class NormalizerTest extends TestCase for ($k=0; $k<3; $k++) { $sample[$k] = rand(1, 100); } + // Last feature's value shared across samples. + $sample[] = 1; + $samples[] = $sample; } @@ -126,6 +129,7 @@ class NormalizerTest extends TestCase return $element < -3 || $element > 3; }); $this->assertCount(0, $errors); + $this->assertEquals(0, $sample[3]); } } }