php-ml/tests/FeatureSelection/VarianceThresholdTest.php
Arkadiusz Kondas 3ba35918a3
Implement VarianceThreshold - simple baseline approach to feature selection. (#228)
* Add sum of squares deviations

* Calculate population variance

* Add VarianceThreshold - feature selection transformer

* Add docs about VarianceThreshold

* Add missing code for pipeline usage
2018-02-10 18:07:09 +01:00

40 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Phpml\Tests\FeatureSelection;
use Phpml\Exception\InvalidArgumentException;
use Phpml\FeatureSelection\VarianceThreshold;
use PHPUnit\Framework\TestCase;
final class VarianceThresholdTest extends TestCase
{
public function testVarianceThreshold(): void
{
$samples = [[0, 0, 1], [0, 1, 0], [1, 0, 0], [0, 1, 1], [0, 1, 0], [0, 1, 1]];
$transformer = new VarianceThreshold(0.8 * (1 - 0.8)); // 80% of samples - boolean features are Bernoulli random variables
$transformer->fit($samples);
$transformer->transform($samples);
// expecting to remove first column
self::assertEquals([[0, 1], [1, 0], [0, 0], [1, 1], [1, 0], [1, 1]], $samples);
}
public function testVarianceThresholdWithZeroThreshold(): void
{
$samples = [[0, 2, 0, 3], [0, 1, 4, 3], [0, 1, 1, 3]];
$transformer = new VarianceThreshold();
$transformer->fit($samples);
$transformer->transform($samples);
self::assertEquals([[2, 0], [1, 4], [1, 1]], $samples);
}
public function testThrowExceptionWhenThresholdBelowZero(): void
{
$this->expectException(InvalidArgumentException::class);
new VarianceThreshold(-0.1);
}
}