implement Median and MostFrequent strategy for imputer

This commit is contained in:
Arkadiusz Kondas 2016-05-08 19:33:39 +02:00
parent a761d0e8f2
commit 65cdfe64b2
5 changed files with 140 additions and 7 deletions

View File

@ -29,8 +29,8 @@ class Mean
*
* @throws InvalidArgumentException
*/
public static function median(array $numbers) {
public static function median(array $numbers)
{
self::checkArrayLength($numbers);
$count = count($numbers);
@ -72,5 +72,4 @@ class Mean
throw InvalidArgumentException::arrayCantBeEmpty();
}
}
}

View File

@ -0,0 +1,21 @@
<?php
declare (strict_types = 1);
namespace Phpml\Preprocessing\Imputer\Strategy;
use Phpml\Preprocessing\Imputer\Strategy;
use Phpml\Math\Statistic\Mean;
class MedianStrategy implements Strategy
{
/**
* @param array $currentAxis
*
* @return float
*/
public function replaceValue(array $currentAxis)
{
return Mean::median($currentAxis);
}
}

View File

@ -0,0 +1,21 @@
<?php
declare (strict_types = 1);
namespace Phpml\Preprocessing\Imputer\Strategy;
use Phpml\Preprocessing\Imputer\Strategy;
use Phpml\Math\Statistic\Mean;
class MostFrequentStrategy implements Strategy
{
/**
* @param array $currentAxis
*
* @return float|mixed
*/
public function replaceValue(array $currentAxis)
{
return Mean::mode($currentAxis);
}
}

View File

@ -8,7 +8,6 @@ use Phpml\Math\Statistic\Mean;
class MeanTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
@ -61,5 +60,4 @@ class MeanTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(6, Mean::mode($numbers));
}
}

View File

@ -6,10 +6,12 @@ namespace tests\Preprocessing;
use Phpml\Preprocessing\Imputer;
use Phpml\Preprocessing\Imputer\Strategy\MeanStrategy;
use Phpml\Preprocessing\Imputer\Strategy\MedianStrategy;
use Phpml\Preprocessing\Imputer\Strategy\MostFrequentStrategy;
class ImputerTest extends \PHPUnit_Framework_TestCase
{
public function testCompletingMissingValuesWithMeanStrategyOnColumnAxis()
public function testComplementsMissingValuesWithMeanStrategyOnColumnAxis()
{
$data = [
[1, null, 3, 4],
@ -31,7 +33,7 @@ class ImputerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($imputeData, $data, '', $delta = 0.01);
}
public function testCompletingMissingValuesWithMeanStrategyOnRowAxis()
public function testComplementsMissingValuesWithMeanStrategyOnRowAxis()
{
$data = [
[1, null, 3, 4],
@ -52,4 +54,96 @@ class ImputerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($imputeData, $data, '', $delta = 0.01);
}
public function testComplementsMissingValuesWithMediaStrategyOnColumnAxis()
{
$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);
$imputer->preprocess($data);
$this->assertEquals($imputeData, $data, '', $delta = 0.01);
}
public function testComplementsMissingValuesWithMediaStrategyOnRowAxis()
{
$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);
$imputer->preprocess($data);
$this->assertEquals($imputeData, $data, '', $delta = 0.01);
}
public function testComplementsMissingValuesWithMostFrequentStrategyOnColumnAxis()
{
$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);
$imputer->preprocess($data);
$this->assertEquals($imputeData, $data);
}
public function testComplementsMissingValuesWithMostFrequentStrategyOnRowAxis()
{
$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);
$imputer->preprocess($data);
$this->assertEquals($imputeData, $data);
}
}