php-ml/tests/Phpml/SupportVectorMachine/SupportVectorMachineTest.php
Tomáš Votruba a348111e97 Add PHPStan and level to max (#168)
* tests: update to PHPUnit 6.0 with rector

* fix namespaces on tests

* composer + tests: use standard test namespace naming

* update travis

* resolve conflict

* phpstan lvl 2

* phpstan lvl 3

* phpstan lvl 4

* phpstan lvl 5

* phpstan lvl 6

* phpstan lvl 7

* level max

* resolve conflict

* [cs] clean empty docs

* composer: bump to PHPUnit 6.4

* cleanup

* composer + travis: add phpstan

* phpstan lvl 1

* composer: update dev deps

* phpstan fixes

* update Contributing with new tools

* docs: link fixes, PHP version update

* composer: drop php-cs-fixer, cs already handled by ecs

* ecs: add old set rules

* [cs] apply rest of rules
2018-01-06 13:09:33 +01:00

109 lines
3.0 KiB
PHP

<?php
declare(strict_types=1);
namespace Phpml\Tests\SupportVectorMachine;
use Phpml\Exception\InvalidArgumentException;
use Phpml\SupportVectorMachine\Kernel;
use Phpml\SupportVectorMachine\SupportVectorMachine;
use Phpml\SupportVectorMachine\Type;
use PHPUnit\Framework\TestCase;
class SupportVectorMachineTest extends TestCase
{
public function testTrainCSVCModelWithLinearKernel(): void
{
$samples = [[1, 3], [1, 4], [2, 4], [3, 1], [4, 1], [4, 2]];
$labels = ['a', 'a', 'a', 'b', 'b', 'b'];
$model =
'svm_type c_svc
kernel_type linear
nr_class 2
total_sv 2
rho 0
label 0 1
nr_sv 1 1
SV
0.25 1:2 2:4
-0.25 1:4 2:2
';
$svm = new SupportVectorMachine(Type::C_SVC, Kernel::LINEAR, 100.0);
$svm->train($samples, $labels);
$this->assertEquals($model, $svm->getModel());
}
public function testPredictSampleWithLinearKernel(): void
{
$samples = [[1, 3], [1, 4], [2, 4], [3, 1], [4, 1], [4, 2]];
$labels = ['a', 'a', 'a', 'b', 'b', 'b'];
$svm = new SupportVectorMachine(Type::C_SVC, Kernel::LINEAR, 100.0);
$svm->train($samples, $labels);
$predictions = $svm->predict([
[3, 2],
[2, 3],
[4, -5],
]);
$this->assertEquals('b', $predictions[0]);
$this->assertEquals('a', $predictions[1]);
$this->assertEquals('b', $predictions[2]);
}
public function testPredictSampleFromMultipleClassWithRbfKernel(): void
{
$samples = [
[1, 3], [1, 4], [1, 4],
[3, 1], [4, 1], [4, 2],
[-3, -1], [-4, -1], [-4, -2],
];
$labels = [
'a', 'a', 'a',
'b', 'b', 'b',
'c', 'c', 'c',
];
$svm = new SupportVectorMachine(Type::C_SVC, Kernel::RBF, 100.0);
$svm->train($samples, $labels);
$predictions = $svm->predict([
[1, 5],
[4, 3],
[-4, -3],
]);
$this->assertEquals('a', $predictions[0]);
$this->assertEquals('b', $predictions[1]);
$this->assertEquals('c', $predictions[2]);
}
public function testThrowExceptionWhenVarPathIsNotWritable(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('is not writable');
$svm = new SupportVectorMachine(Type::C_SVC, Kernel::RBF);
$svm->setVarPath('var-path');
}
public function testThrowExceptionWhenBinPathDoesNotExist(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('does not exist');
$svm = new SupportVectorMachine(Type::C_SVC, Kernel::RBF);
$svm->setBinPath('bin-path');
}
public function testThrowExceptionWhenFileIsNotFoundInBinPath(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('not found');
$svm = new SupportVectorMachine(Type::C_SVC, Kernel::RBF);
$svm->setBinPath('var');
}
}