mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2025-01-10 00:37:55 +00:00
726cf4cddf
* 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
139 lines
3.3 KiB
PHP
139 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace tests\Phpml\Preprocessing;
|
|
|
|
use Phpml\Preprocessing\Normalizer;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class NormalizerTest extends TestCase
|
|
{
|
|
/**
|
|
* @expectedException \Phpml\Exception\NormalizerException
|
|
*/
|
|
public function testThrowExceptionOnInvalidNorm(): void
|
|
{
|
|
new Normalizer(99);
|
|
}
|
|
|
|
public function testNormalizeSamplesWithL2Norm(): void
|
|
{
|
|
$samples = [
|
|
[1, -1, 2],
|
|
[2, 0, 0],
|
|
[0, 1, -1],
|
|
];
|
|
|
|
$normalized = [
|
|
[0.4, -0.4, 0.81],
|
|
[1.0, 0.0, 0.0],
|
|
[0.0, 0.7, -0.7],
|
|
];
|
|
|
|
$normalizer = new Normalizer();
|
|
$normalizer->transform($samples);
|
|
|
|
$this->assertEquals($normalized, $samples, '', $delta = 0.01);
|
|
}
|
|
|
|
public function testNormalizeSamplesWithL1Norm(): void
|
|
{
|
|
$samples = [
|
|
[1, -1, 2],
|
|
[2, 0, 0],
|
|
[0, 1, -1],
|
|
];
|
|
|
|
$normalized = [
|
|
[0.25, -0.25, 0.5],
|
|
[1.0, 0.0, 0.0],
|
|
[0.0, 0.5, -0.5],
|
|
];
|
|
|
|
$normalizer = new Normalizer(Normalizer::NORM_L1);
|
|
$normalizer->transform($samples);
|
|
|
|
$this->assertEquals($normalized, $samples, '', $delta = 0.01);
|
|
}
|
|
|
|
public function testFitNotChangeNormalizerBehavior(): void
|
|
{
|
|
$samples = [
|
|
[1, -1, 2],
|
|
[2, 0, 0],
|
|
[0, 1, -1],
|
|
];
|
|
|
|
$normalized = [
|
|
[0.4, -0.4, 0.81],
|
|
[1.0, 0.0, 0.0],
|
|
[0.0, 0.7, -0.7],
|
|
];
|
|
|
|
$normalizer = new Normalizer();
|
|
$normalizer->transform($samples);
|
|
|
|
$this->assertEquals($normalized, $samples, '', $delta = 0.01);
|
|
|
|
$normalizer->fit($samples);
|
|
|
|
$this->assertEquals($normalized, $samples, '', $delta = 0.01);
|
|
}
|
|
|
|
public function testL1NormWithZeroSumCondition(): void
|
|
{
|
|
$samples = [
|
|
[0, 0, 0],
|
|
[2, 0, 0],
|
|
[0, 1, -1],
|
|
];
|
|
|
|
$normalized = [
|
|
[0.33, 0.33, 0.33],
|
|
[1.0, 0.0, 0.0],
|
|
[0.0, 0.5, -0.5],
|
|
];
|
|
|
|
$normalizer = new Normalizer(Normalizer::NORM_L1);
|
|
$normalizer->transform($samples);
|
|
|
|
$this->assertEquals($normalized, $samples, '', $delta = 0.01);
|
|
}
|
|
|
|
public function testStandardNorm(): void
|
|
{
|
|
// Generate 10 random vectors of length 3
|
|
$samples = [];
|
|
srand(time());
|
|
for ($i = 0; $i < 10; ++$i) {
|
|
$sample = array_fill(0, 3, 0);
|
|
for ($k = 0; $k < 3; ++$k) {
|
|
$sample[$k] = random_int(1, 100);
|
|
}
|
|
|
|
// Last feature's value shared across samples.
|
|
$sample[] = 1;
|
|
|
|
$samples[] = $sample;
|
|
}
|
|
|
|
// Use standard normalization
|
|
$normalizer = new Normalizer(Normalizer::NORM_STD);
|
|
$normalizer->transform($samples);
|
|
|
|
// Values in the vector should be some value between -3 and +3
|
|
$this->assertCount(10, $samples);
|
|
foreach ($samples as $sample) {
|
|
$errors = array_filter(
|
|
$sample,
|
|
function ($element) {
|
|
return $element < -3 || $element > 3;
|
|
}
|
|
);
|
|
$this->assertCount(0, $errors);
|
|
$this->assertEquals(0, $sample[3]);
|
|
}
|
|
}
|
|
}
|