mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2025-04-08 19:21:52 +00:00
* travis: move coveralls here, decouple from package * composer: use PSR4 * phpunit: simpler config * travis: add ecs run * composer: add ecs dev * use standard vendor/bin directory for dependency bins, confuses with local bins and require gitignore handling * ecs: add PSR2 * [cs] PSR2 spacing fixes * [cs] PSR2 class name fix * [cs] PHP7 fixes - return semicolon spaces, old rand functions, typehints * [cs] fix less strict typehints * fix typehints to make tests pass * ecs: ignore typehint-less elements * [cs] standardize arrays * [cs] standardize docblock, remove unused comments * [cs] use self where possible * [cs] sort class elements, from public to private * [cs] do not use yoda (found less yoda-cases, than non-yoda) * space * [cs] do not assign in condition * [cs] use namespace imports if possible * [cs] use ::class over strings * [cs] fix defaults for arrays properties, properties and constants single spacing * cleanup ecs comments * [cs] use item per line in multi-items array * missing line * misc * rebase
132 lines
2.9 KiB
PHP
132 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Phpml\Preprocessing;
|
|
|
|
use Phpml\Exception\NormalizerException;
|
|
use Phpml\Math\Statistic\Mean;
|
|
use Phpml\Math\Statistic\StandardDeviation;
|
|
|
|
class Normalizer implements Preprocessor
|
|
{
|
|
public const NORM_L1 = 1;
|
|
|
|
public const NORM_L2 = 2;
|
|
|
|
public const NORM_STD = 3;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
private $norm;
|
|
|
|
/**
|
|
* @var bool
|
|
*/
|
|
private $fitted = false;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
private $std = [];
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
private $mean = [];
|
|
|
|
/**
|
|
* @throws NormalizerException
|
|
*/
|
|
public function __construct(int $norm = self::NORM_L2)
|
|
{
|
|
if (!in_array($norm, [self::NORM_L1, self::NORM_L2, self::NORM_STD])) {
|
|
throw NormalizerException::unknownNorm();
|
|
}
|
|
|
|
$this->norm = $norm;
|
|
}
|
|
|
|
public function fit(array $samples): void
|
|
{
|
|
if ($this->fitted) {
|
|
return;
|
|
}
|
|
|
|
if ($this->norm == self::NORM_STD) {
|
|
$features = range(0, count($samples[0]) - 1);
|
|
foreach ($features as $i) {
|
|
$values = array_column($samples, $i);
|
|
$this->std[$i] = StandardDeviation::population($values);
|
|
$this->mean[$i] = Mean::arithmetic($values);
|
|
}
|
|
}
|
|
|
|
$this->fitted = true;
|
|
}
|
|
|
|
public function transform(array &$samples): void
|
|
{
|
|
$methods = [
|
|
self::NORM_L1 => 'normalizeL1',
|
|
self::NORM_L2 => 'normalizeL2',
|
|
self::NORM_STD => 'normalizeSTD',
|
|
];
|
|
$method = $methods[$this->norm];
|
|
|
|
$this->fit($samples);
|
|
|
|
foreach ($samples as &$sample) {
|
|
$this->{$method}($sample);
|
|
}
|
|
}
|
|
|
|
private function normalizeL1(array &$sample): void
|
|
{
|
|
$norm1 = 0;
|
|
foreach ($sample as $feature) {
|
|
$norm1 += abs($feature);
|
|
}
|
|
|
|
if ($norm1 == 0) {
|
|
$count = count($sample);
|
|
$sample = array_fill(0, $count, 1.0 / $count);
|
|
} else {
|
|
foreach ($sample as &$feature) {
|
|
$feature /= $norm1;
|
|
}
|
|
}
|
|
}
|
|
|
|
private function normalizeL2(array &$sample): void
|
|
{
|
|
$norm2 = 0;
|
|
foreach ($sample as $feature) {
|
|
$norm2 += $feature * $feature;
|
|
}
|
|
|
|
$norm2 = sqrt((float) $norm2);
|
|
|
|
if ($norm2 == 0) {
|
|
$sample = array_fill(0, count($sample), 1);
|
|
} else {
|
|
foreach ($sample as &$feature) {
|
|
$feature /= $norm2;
|
|
}
|
|
}
|
|
}
|
|
|
|
private function normalizeSTD(array &$sample): void
|
|
{
|
|
foreach ($sample as $i => $val) {
|
|
if ($this->std[$i] != 0) {
|
|
$sample[$i] = ($sample[$i] - $this->mean[$i]) / $this->std[$i];
|
|
} else {
|
|
// Same value for all samples.
|
|
$sample[$i] = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|