php-ml/tests/Phpml/Preprocessing/ImputerTest.php
Tomáš Votruba a348111e97 Add PHPStan and level to max (#168)
* tests: update to PHPUnit 6.0 with rector

* fix namespaces on tests

* composer + tests: use standard test namespace naming

* update travis

* resolve conflict

* phpstan lvl 2

* phpstan lvl 3

* phpstan lvl 4

* phpstan lvl 5

* phpstan lvl 6

* phpstan lvl 7

* level max

* resolve conflict

* [cs] clean empty docs

* composer: bump to PHPUnit 6.4

* cleanup

* composer + travis: add phpstan

* phpstan lvl 1

* composer: update dev deps

* phpstan fixes

* update Contributing with new tools

* docs: link fixes, PHP version update

* composer: drop php-cs-fixer, cs already handled by ecs

* ecs: add old set rules

* [cs] apply rest of rules
2018-01-06 13:09:33 +01:00

177 lines
4.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Phpml\Tests\Preprocessing;
use Phpml\Preprocessing\Imputer;
use Phpml\Preprocessing\Imputer\Strategy\MeanStrategy;
use Phpml\Preprocessing\Imputer\Strategy\MedianStrategy;
use Phpml\Preprocessing\Imputer\Strategy\MostFrequentStrategy;
use PHPUnit\Framework\TestCase;
class ImputerTest extends TestCase
{
public function testComplementsMissingValuesWithMeanStrategyOnColumnAxis(): void
{
$data = [
[1, null, 3, 4],
[4, 3, 2, 1],
[null, 6, 7, 8],
[8, 7, null, 5],
];
$imputeData = [
[1, 5.33, 3, 4],
[4, 3, 2, 1],
[4.33, 6, 7, 8],
[8, 7, 4, 5],
];
$imputer = new Imputer(null, new MeanStrategy(), Imputer::AXIS_COLUMN, $data);
$imputer->transform($data);
$this->assertEquals($imputeData, $data, '', $delta = 0.01);
}
public function testComplementsMissingValuesWithMeanStrategyOnRowAxis(): void
{
$data = [
[1, null, 3, 4],
[4, 3, 2, 1],
[null, 6, 7, 8],
[8, 7, null, 5],
];
$imputeData = [
[1, 2.66, 3, 4],
[4, 3, 2, 1],
[7, 6, 7, 8],
[8, 7, 6.66, 5],
];
$imputer = new Imputer(null, new MeanStrategy(), Imputer::AXIS_ROW, $data);
$imputer->transform($data);
$this->assertEquals($imputeData, $data, '', $delta = 0.01);
}
public function testComplementsMissingValuesWithMediaStrategyOnColumnAxis(): void
{
$data = [
[1, null, 3, 4],
[4, 3, 2, 1],
[null, 6, 7, 8],
[8, 7, null, 5],
];
$imputeData = [
[1, 6, 3, 4],
[4, 3, 2, 1],
[4, 6, 7, 8],
[8, 7, 3, 5],
];
$imputer = new Imputer(null, new MedianStrategy(), Imputer::AXIS_COLUMN, $data);
$imputer->transform($data);
$this->assertEquals($imputeData, $data, '', $delta = 0.01);
}
public function testComplementsMissingValuesWithMediaStrategyOnRowAxis(): void
{
$data = [
[1, null, 3, 4],
[4, 3, 2, 1],
[null, 6, 7, 8],
[8, 7, null, 5],
];
$imputeData = [
[1, 3, 3, 4],
[4, 3, 2, 1],
[7, 6, 7, 8],
[8, 7, 7, 5],
];
$imputer = new Imputer(null, new MedianStrategy(), Imputer::AXIS_ROW, $data);
$imputer->transform($data);
$this->assertEquals($imputeData, $data, '', $delta = 0.01);
}
public function testComplementsMissingValuesWithMostFrequentStrategyOnColumnAxis(): void
{
$data = [
[1, null, 3, 4],
[4, 3, 2, 1],
[null, 6, 7, 8],
[8, 7, null, 5],
[8, 3, 2, 5],
];
$imputeData = [
[1, 3, 3, 4],
[4, 3, 2, 1],
[8, 6, 7, 8],
[8, 7, 2, 5],
[8, 3, 2, 5],
];
$imputer = new Imputer(null, new MostFrequentStrategy(), Imputer::AXIS_COLUMN, $data);
$imputer->transform($data);
$this->assertEquals($imputeData, $data);
}
public function testComplementsMissingValuesWithMostFrequentStrategyOnRowAxis(): void
{
$data = [
[1, null, 3, 4, 3],
[4, 3, 2, 1, 7],
[null, 6, 7, 8, 6],
[8, 7, null, 5, 5],
[8, 3, 2, 5, 4],
];
$imputeData = [
[1, 3, 3, 4, 3],
[4, 3, 2, 1, 7],
[6, 6, 7, 8, 6],
[8, 7, 5, 5, 5],
[8, 3, 2, 5, 4],
];
$imputer = new Imputer(null, new MostFrequentStrategy(), Imputer::AXIS_ROW, $data);
$imputer->transform($data);
$this->assertEquals($imputeData, $data);
}
public function testImputerWorksOnFitSamples(): void
{
$trainData = [
[1, 3, 4],
[6, 7, 8],
[8, 7, 5],
];
$data = [
[1, 3, null],
[6, null, 8],
[null, 7, 5],
];
$imputeData = [
[1, 3, 5.66],
[6, 5.66, 8],
[5, 7, 5],
];
$imputer = new Imputer(null, new MeanStrategy(), Imputer::AXIS_COLUMN, $trainData);
$imputer->transform($data);
$this->assertEquals($imputeData, $data, '', $delta = 0.01);
}
}