php-ml/tests/Phpml/DimensionReduction/PCATest.php
Tomáš Votruba 726cf4cddf Added EasyCodingStandard + lots of code fixes (#156)
* travis: move coveralls here, decouple from package

* composer: use PSR4

* phpunit: simpler config

* travis: add ecs run

* composer: add ecs dev

* use standard vendor/bin directory for dependency bins, confuses with local bins and require gitignore handling

* ecs: add PSR2

* [cs] PSR2 spacing fixes

* [cs] PSR2 class name fix

* [cs] PHP7 fixes - return semicolon spaces, old rand functions, typehints

* [cs] fix less strict typehints

* fix typehints to make tests pass

* ecs: ignore typehint-less elements

* [cs] standardize arrays

* [cs] standardize docblock, remove unused comments

* [cs] use self where possible

* [cs] sort class elements, from public to private

* [cs] do not use yoda (found less yoda-cases, than non-yoda)

* space

* [cs] do not assign in condition

* [cs] use namespace imports if possible

* [cs] use ::class over strings

* [cs] fix defaults for arrays properties, properties and constants single spacing

* cleanup ecs comments

* [cs] use item per line in multi-items array

* missing line

* misc

* rebase
2017-11-22 22:16:10 +01:00

58 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace tests\Phpml\DimensionReduction;
use Phpml\DimensionReduction\PCA;
use PHPUnit\Framework\TestCase;
class PCATest extends TestCase
{
public function testPCA(): void
{
// 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],
[1.1, 0.9],
];
$transformed = [
[-0.827970186], [1.77758033], [-0.992197494],
[-0.274210416], [-1.67580142], [-0.912949103], [0.0991094375],
[1.14457216], [0.438046137], [1.22382056], ];
$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
array_map(function ($val1, $val2) use ($epsilon): void {
$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]]];
$newRow2 = $pca->transform($row);
array_map(function ($val1, $val2) use ($epsilon): void {
$this->assertEquals(abs($val1), abs($val2), '', $epsilon);
}, $newRow, $newRow2);
}
}
}