2017-04-23 07:03:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-01-06 12:09:33 +00:00
|
|
|
namespace Phpml\Tests\DimensionReduction;
|
2017-04-23 07:03:30 +00:00
|
|
|
|
|
|
|
use Phpml\DimensionReduction\PCA;
|
2018-03-06 22:26:36 +00:00
|
|
|
use Phpml\Exception\InvalidArgumentException;
|
|
|
|
use Phpml\Exception\InvalidOperationException;
|
2017-04-23 07:03:30 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
class PCATest extends TestCase
|
|
|
|
{
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testPCA(): void
|
2017-04-23 07:03:30 +00:00
|
|
|
{
|
|
|
|
// Acceptable error
|
|
|
|
$epsilon = 0.001;
|
|
|
|
|
|
|
|
// First a simple example whose result is known and given in
|
|
|
|
// http://www.cs.otago.ac.nz/cosc453/student_tutorials/principal_components.pdf
|
|
|
|
$data = [
|
|
|
|
[2.5, 2.4],
|
|
|
|
[0.5, 0.7],
|
|
|
|
[2.2, 2.9],
|
|
|
|
[1.9, 2.2],
|
|
|
|
[3.1, 3.0],
|
|
|
|
[2.3, 2.7],
|
|
|
|
[2.0, 1.6],
|
|
|
|
[1.0, 1.1],
|
|
|
|
[1.5, 1.6],
|
2017-11-22 21:16:10 +00:00
|
|
|
[1.1, 0.9],
|
2017-04-23 07:03:30 +00:00
|
|
|
];
|
|
|
|
$transformed = [
|
|
|
|
[-0.827970186], [1.77758033], [-0.992197494],
|
|
|
|
[-0.274210416], [-1.67580142], [-0.912949103], [0.0991094375],
|
2017-11-22 21:16:10 +00:00
|
|
|
[1.14457216], [0.438046137], [1.22382056], ];
|
2017-04-23 07:03:30 +00:00
|
|
|
|
|
|
|
$pca = new PCA(0.90);
|
|
|
|
$reducedData = $pca->fit($data);
|
|
|
|
|
|
|
|
// Due to the fact that the sign of values can be flipped
|
|
|
|
// during the calculation of eigenValues, we have to compare
|
|
|
|
// absolute value of the values
|
2017-11-14 20:21:23 +00:00
|
|
|
array_map(function ($val1, $val2) use ($epsilon): void {
|
2017-04-23 07:03:30 +00:00
|
|
|
$this->assertEquals(abs($val1), abs($val2), '', $epsilon);
|
|
|
|
}, $transformed, $reducedData);
|
|
|
|
|
|
|
|
// Test fitted PCA object to transform an arbitrary sample of the
|
|
|
|
// same dimensionality with the original dataset
|
|
|
|
foreach ($data as $i => $row) {
|
|
|
|
$newRow = [[$transformed[$i]]];
|
2017-08-17 06:50:37 +00:00
|
|
|
$newRow2 = $pca->transform($row);
|
2017-04-23 07:03:30 +00:00
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
array_map(function ($val1, $val2) use ($epsilon): void {
|
2017-04-23 07:03:30 +00:00
|
|
|
$this->assertEquals(abs($val1), abs($val2), '', $epsilon);
|
|
|
|
}, $newRow, $newRow2);
|
|
|
|
}
|
|
|
|
}
|
2018-03-06 22:26:36 +00:00
|
|
|
|
|
|
|
public function testPCAThrowWhenTotalVarianceOutOfRange(): void
|
|
|
|
{
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$pca = new PCA(0, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPCAThrowWhenNumFeaturesOutOfRange(): void
|
|
|
|
{
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$pca = new PCA(null, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPCAThrowWhenParameterNotSpecified(): void
|
|
|
|
{
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$pca = new PCA();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPCAThrowWhenBothParameterSpecified(): void
|
|
|
|
{
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$pca = new PCA(0.9, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTransformThrowWhenNotFitted(): void
|
|
|
|
{
|
|
|
|
$samples = [
|
|
|
|
[1, 0],
|
|
|
|
[1, 1],
|
|
|
|
];
|
|
|
|
|
|
|
|
$pca = new PCA(0.9);
|
|
|
|
|
|
|
|
$this->expectException(InvalidOperationException::class);
|
|
|
|
$pca->transform($samples);
|
|
|
|
}
|
2017-04-23 07:03:30 +00:00
|
|
|
}
|