mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-11-22 04:55:10 +00:00
3ba35918a3
* Add sum of squares deviations * Calculate population variance * Add VarianceThreshold - feature selection transformer * Add docs about VarianceThreshold * Add missing code for pipeline usage
40 lines
1.2 KiB
PHP
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);
|
|
}
|
|
}
|