Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
50.00% |
2 / 4 |
CRAP | |
88.46% |
23 / 26 |
| Normalizer | |
0.00% |
0 / 1 |
|
50.00% |
2 / 4 |
12.22 | |
88.46% |
23 / 26 |
| __construct | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
| preprocess | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
| normalizeL1 | |
0.00% |
0 / 1 |
4.18 | |
77.78% |
7 / 9 |
|||
| normalizeL2 | |
0.00% |
0 / 1 |
4.02 | |
88.89% |
8 / 9 |
|||
| <?php | |
| declare (strict_types = 1); | |
| namespace Phpml\Preprocessing; | |
| use Phpml\Exception\NormalizerException; | |
| class Normalizer implements Preprocessor | |
| { | |
| const NORM_L1 = 1; | |
| const NORM_L2 = 2; | |
| /** | |
| * @var int | |
| */ | |
| private $norm; | |
| /** | |
| * @param int $norm | |
| * | |
| * @throws NormalizerException | |
| */ | |
| public function __construct(int $norm = self::NORM_L2) | |
| { | |
| if (!in_array($norm, [self::NORM_L1, self::NORM_L2])) { | |
| throw NormalizerException::unknownNorm(); | |
| } | |
| $this->norm = $norm; | |
| } | |
| /** | |
| * @param array $samples | |
| */ | |
| public function preprocess(array &$samples) | |
| { | |
| $method = sprintf('normalizeL%s', $this->norm); | |
| foreach ($samples as &$sample) { | |
| $this->$method($sample); | |
| } | |
| } | |
| /** | |
| * @param array $sample | |
| */ | |
| private function normalizeL1(array &$sample) | |
| { | |
| $norm1 = 0; | |
| foreach ($sample as $feature) { | |
| $norm1 += abs($feature); | |
| } | |
| if (0 == $norm1) { | |
| $count = count($sample); | |
| $sample = array_fill(0, $count, 1.0 / $count); | |
| } else { | |
| foreach ($sample as &$feature) { | |
| $feature = $feature / $norm1; | |
| } | |
| } | |
| } | |
| /** | |
| * @param array $sample | |
| */ | |
| private function normalizeL2(array &$sample) | |
| { | |
| $norm2 = 0; | |
| foreach ($sample as $feature) { | |
| $norm2 += $feature * $feature; | |
| } | |
| $norm2 = sqrt($norm2); | |
| if (0 == $norm2) { | |
| $sample = array_fill(0, count($sample), 1); | |
| } else { | |
| foreach ($sample as &$feature) { | |
| $feature = $feature / $norm2; | |
| } | |
| } | |
| } | |
| } |