mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2025-01-09 00:20:53 +00:00
implement Median and MostFrequent strategy for imputer
This commit is contained in:
parent
a761d0e8f2
commit
65cdfe64b2
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
21
src/Phpml/Preprocessing/Imputer/Strategy/MedianStrategy.php
Normal file
21
src/Phpml/Preprocessing/Imputer/Strategy/MedianStrategy.php
Normal 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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user