2016-05-08 18:35:01 +00:00
|
|
|
<?php
|
|
|
|
|
2016-11-20 21:53:17 +00:00
|
|
|
declare(strict_types=1);
|
2016-05-08 18:35:01 +00:00
|
|
|
|
|
|
|
namespace tests\Preprocessing;
|
|
|
|
|
|
|
|
use Phpml\Preprocessing\Normalizer;
|
2017-02-03 11:58:25 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-05-08 18:35:01 +00:00
|
|
|
|
2017-02-03 11:58:25 +00:00
|
|
|
class NormalizerTest extends TestCase
|
2016-05-08 18:35:01 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @expectedException \Phpml\Exception\NormalizerException
|
|
|
|
*/
|
|
|
|
public function testThrowExceptionOnInvalidNorm()
|
|
|
|
{
|
|
|
|
new Normalizer(99);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNormalizeSamplesWithL2Norm()
|
|
|
|
{
|
|
|
|
$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();
|
2016-06-16 08:03:57 +00:00
|
|
|
$normalizer->transform($samples);
|
2016-05-08 18:35:01 +00:00
|
|
|
|
|
|
|
$this->assertEquals($normalized, $samples, '', $delta = 0.01);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNormalizeSamplesWithL1Norm()
|
|
|
|
{
|
|
|
|
$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);
|
2016-06-16 08:03:57 +00:00
|
|
|
$normalizer->transform($samples);
|
2016-05-08 18:35:01 +00:00
|
|
|
|
|
|
|
$this->assertEquals($normalized, $samples, '', $delta = 0.01);
|
|
|
|
}
|
2016-06-16 22:23:27 +00:00
|
|
|
|
|
|
|
public function testFitNotChangeNormalizerBehavior()
|
|
|
|
{
|
|
|
|
$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);
|
|
|
|
}
|
2016-07-24 11:42:50 +00:00
|
|
|
|
|
|
|
public function testL1NormWithZeroSumCondition()
|
|
|
|
{
|
|
|
|
$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);
|
|
|
|
}
|
2016-05-08 18:35:01 +00:00
|
|
|
}
|