php-ml/tests/Phpml/SupportVectorMachine/SupportVectorMachineTest.php

109 lines
3.0 KiB
PHP
Raw Normal View History

2016-05-05 21:29:11 +00:00
<?php
2016-11-20 21:53:17 +00:00
declare(strict_types=1);
2016-05-05 21:29:11 +00:00
namespace Phpml\Tests\SupportVectorMachine;
2016-05-05 21:29:11 +00:00
use Phpml\Exception\InvalidArgumentException;
2016-05-05 21:29:11 +00:00
use Phpml\SupportVectorMachine\Kernel;
use Phpml\SupportVectorMachine\SupportVectorMachine;
use Phpml\SupportVectorMachine\Type;
2017-02-03 11:58:25 +00:00
use PHPUnit\Framework\TestCase;
2016-05-05 21:29:11 +00:00
2017-02-03 11:58:25 +00:00
class SupportVectorMachineTest extends TestCase
2016-05-05 21:29:11 +00:00
{
public function testTrainCSVCModelWithLinearKernel(): void
2016-05-05 21:29:11 +00:00
{
$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
2016-05-06 20:33:04 +00:00
0.25 1:2 2:4
-0.25 1:4 2:2
2016-05-05 21:29:11 +00:00
';
$svm = new SupportVectorMachine(Type::C_SVC, Kernel::LINEAR, 100.0);
$svm->train($samples, $labels);
$this->assertEquals($model, $svm->getModel());
}
2016-05-06 20:55:41 +00:00
public function testPredictSampleWithLinearKernel(): void
2016-05-06 20:55:41 +00:00
{
$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]);
}
2016-05-07 12:08:09 +00:00
public function testPredictSampleFromMultipleClassWithRbfKernel(): void
2016-05-07 12:08:09 +00:00
{
$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');
}
2016-05-05 21:29:11 +00:00
}