mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-11-26 06:46:45 +00:00
726cf4cddf
* 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
61 lines
1.9 KiB
PHP
61 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace tests\Phpml\Regression;
|
|
|
|
use Phpml\ModelManager;
|
|
use Phpml\Regression\SVR;
|
|
use Phpml\SupportVectorMachine\Kernel;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class SVRTest extends TestCase
|
|
{
|
|
public function testPredictSingleFeatureSamples(): void
|
|
{
|
|
$delta = 0.01;
|
|
|
|
$samples = [[60], [61], [62], [63], [65]];
|
|
$targets = [3.1, 3.6, 3.8, 4, 4.1];
|
|
|
|
$regression = new SVR(Kernel::LINEAR);
|
|
$regression->train($samples, $targets);
|
|
|
|
$this->assertEquals(4.03, $regression->predict([64]), '', $delta);
|
|
}
|
|
|
|
public function testPredictMultiFeaturesSamples(): void
|
|
{
|
|
$delta = 0.01;
|
|
|
|
$samples = [[73676, 1996], [77006, 1998], [10565, 2000], [146088, 1995], [15000, 2001], [65940, 2000], [9300, 2000], [93739, 1996], [153260, 1994], [17764, 2002], [57000, 1998], [15000, 2000]];
|
|
$targets = [2000, 2750, 15500, 960, 4400, 8800, 7100, 2550, 1025, 5900, 4600, 4400];
|
|
|
|
$regression = new SVR(Kernel::LINEAR);
|
|
$regression->train($samples, $targets);
|
|
|
|
$this->assertEquals([4109.82, 4112.28], $regression->predict([[60000, 1996], [60000, 2000]]), '', $delta);
|
|
}
|
|
|
|
public function testSaveAndRestore(): void
|
|
{
|
|
$samples = [[60], [61], [62], [63], [65]];
|
|
$targets = [3.1, 3.6, 3.8, 4, 4.1];
|
|
|
|
$regression = new SVR(Kernel::LINEAR);
|
|
$regression->train($samples, $targets);
|
|
|
|
$testSamples = [64];
|
|
$predicted = $regression->predict($testSamples);
|
|
|
|
$filename = 'svr-test'.random_int(100, 999).'-'.uniqid();
|
|
$filepath = tempnam(sys_get_temp_dir(), $filename);
|
|
$modelManager = new ModelManager();
|
|
$modelManager->saveToFile($regression, $filepath);
|
|
|
|
$restoredRegression = $modelManager->restoreFromFile($filepath);
|
|
$this->assertEquals($regression, $restoredRegression);
|
|
$this->assertEquals($predicted, $restoredRegression->predict($testSamples));
|
|
}
|
|
}
|