php-ml/tests/Regression/SVRTest.php

61 lines
2.0 KiB
PHP
Raw Normal View History

2016-05-07 21:04:58 +00:00
<?php
2016-11-20 21:53:17 +00:00
declare(strict_types=1);
2016-05-07 21:04:58 +00:00
namespace Phpml\Tests\Regression;
2016-05-07 21:04:58 +00:00
use Phpml\ModelManager;
2016-05-07 21:04:58 +00:00
use Phpml\Regression\SVR;
use Phpml\SupportVectorMachine\Kernel;
2017-02-03 11:58:25 +00:00
use PHPUnit\Framework\TestCase;
2016-05-07 21:04:58 +00:00
2017-02-03 11:58:25 +00:00
class SVRTest extends TestCase
2016-05-07 21:04:58 +00:00
{
public function testPredictSingleFeatureSamples(): void
2016-05-07 21:04:58 +00:00
{
$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);
self::assertEqualsWithDelta(4.03, $regression->predict([64]), $delta);
2016-05-07 21:04:58 +00:00
}
public function testPredictMultiFeaturesSamples(): void
2016-05-07 21:04:58 +00:00
{
$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);
self::assertEqualsWithDelta([4109.82, 4112.28], $regression->predict([[60000, 1996], [60000, 2000]]), $delta);
2016-05-07 21:04:58 +00:00
}
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);
2018-10-28 06:44:52 +00:00
$filename = 'svr-test'.random_int(100, 999).'-'.uniqid('', false);
$filepath = (string) tempnam(sys_get_temp_dir(), $filename);
$modelManager = new ModelManager();
$modelManager->saveToFile($regression, $filepath);
$restoredRegression = $modelManager->restoreFromFile($filepath);
2018-10-28 06:44:52 +00:00
self::assertEquals($regression, $restoredRegression);
self::assertEquals($predicted, $restoredRegression->predict($testSamples));
}
2016-05-07 21:04:58 +00:00
}