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\SupportVectorMachine;
|
|
|
|
|
2017-09-02 20:44:19 +00:00
|
|
|
use Phpml\Exception\InvalidArgumentException;
|
2017-03-17 10:44:45 +00:00
|
|
|
use Phpml\Helper\Trainable;
|
|
|
|
|
2016-05-05 21:29:11 +00:00
|
|
|
class SupportVectorMachine
|
|
|
|
{
|
2017-04-19 20:28:07 +00:00
|
|
|
use Trainable;
|
2017-05-13 10:58:06 +00:00
|
|
|
|
|
|
|
/**
|
2016-05-05 21:29:11 +00:00
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $kernel;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var float
|
|
|
|
*/
|
|
|
|
private $cost;
|
|
|
|
|
2016-05-07 20:17:12 +00:00
|
|
|
/**
|
|
|
|
* @var float
|
|
|
|
*/
|
|
|
|
private $nu;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $degree;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var float
|
|
|
|
*/
|
|
|
|
private $gamma;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var float
|
|
|
|
*/
|
|
|
|
private $coef0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var float
|
|
|
|
*/
|
|
|
|
private $epsilon;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var float
|
|
|
|
*/
|
|
|
|
private $tolerance;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $cacheSize;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $shrinking;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $probabilityEstimates;
|
|
|
|
|
2016-05-05 21:29:11 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $binPath;
|
|
|
|
|
|
|
|
/**
|
2016-05-06 20:33:04 +00:00
|
|
|
* @var string
|
2016-05-05 21:29:11 +00:00
|
|
|
*/
|
|
|
|
private $varPath;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $model;
|
|
|
|
|
2016-05-06 20:55:41 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-03-17 10:44:45 +00:00
|
|
|
private $targets = [];
|
2016-05-06 20:55:41 +00:00
|
|
|
|
2016-05-07 20:17:12 +00:00
|
|
|
public function __construct(
|
2017-07-26 06:24:47 +00:00
|
|
|
int $type,
|
|
|
|
int $kernel,
|
|
|
|
float $cost = 1.0,
|
|
|
|
float $nu = 0.5,
|
|
|
|
int $degree = 3,
|
|
|
|
float $gamma = null,
|
|
|
|
float $coef0 = 0.0,
|
|
|
|
float $epsilon = 0.1,
|
|
|
|
float $tolerance = 0.001,
|
|
|
|
int $cacheSize = 100,
|
|
|
|
bool $shrinking = true,
|
|
|
|
bool $probabilityEstimates = false
|
2016-05-07 20:17:12 +00:00
|
|
|
) {
|
2016-05-05 21:29:11 +00:00
|
|
|
$this->type = $type;
|
|
|
|
$this->kernel = $kernel;
|
|
|
|
$this->cost = $cost;
|
2016-05-07 20:17:12 +00:00
|
|
|
$this->nu = $nu;
|
|
|
|
$this->degree = $degree;
|
|
|
|
$this->gamma = $gamma;
|
|
|
|
$this->coef0 = $coef0;
|
|
|
|
$this->epsilon = $epsilon;
|
|
|
|
$this->tolerance = $tolerance;
|
|
|
|
$this->cacheSize = $cacheSize;
|
|
|
|
$this->shrinking = $shrinking;
|
|
|
|
$this->probabilityEstimates = $probabilityEstimates;
|
2016-05-05 21:29:11 +00:00
|
|
|
|
2016-12-12 17:45:14 +00:00
|
|
|
$rootPath = realpath(implode(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', '..'])).DIRECTORY_SEPARATOR;
|
2016-05-05 21:29:11 +00:00
|
|
|
|
|
|
|
$this->binPath = $rootPath.'bin'.DIRECTORY_SEPARATOR.'libsvm'.DIRECTORY_SEPARATOR;
|
|
|
|
$this->varPath = $rootPath.'var'.DIRECTORY_SEPARATOR;
|
|
|
|
}
|
|
|
|
|
2017-05-13 10:58:06 +00:00
|
|
|
public function setBinPath(string $binPath)
|
|
|
|
{
|
2017-09-02 20:44:19 +00:00
|
|
|
$this->ensureDirectorySeparator($binPath);
|
|
|
|
$this->verifyBinPath($binPath);
|
2017-05-13 10:58:06 +00:00
|
|
|
|
2017-09-02 20:44:19 +00:00
|
|
|
$this->binPath = $binPath;
|
2017-05-13 10:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setVarPath(string $varPath)
|
|
|
|
{
|
2017-09-02 20:44:19 +00:00
|
|
|
if (!is_writable($varPath)) {
|
|
|
|
throw InvalidArgumentException::pathNotWritable($varPath);
|
|
|
|
}
|
2017-05-13 10:58:06 +00:00
|
|
|
|
2017-09-02 20:44:19 +00:00
|
|
|
$this->ensureDirectorySeparator($varPath);
|
|
|
|
$this->varPath = $varPath;
|
2017-05-13 10:58:06 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 10:44:45 +00:00
|
|
|
public function train(array $samples, array $targets)
|
2016-05-05 21:29:11 +00:00
|
|
|
{
|
2017-03-17 10:44:45 +00:00
|
|
|
$this->samples = array_merge($this->samples, $samples);
|
|
|
|
$this->targets = array_merge($this->targets, $targets);
|
|
|
|
|
|
|
|
$trainingSet = DataTransformer::trainingSet($this->samples, $this->targets, in_array($this->type, [Type::EPSILON_SVR, Type::NU_SVR]));
|
2016-12-06 07:50:18 +00:00
|
|
|
file_put_contents($trainingSetFileName = $this->varPath.uniqid('phpml', true), $trainingSet);
|
2016-05-05 21:29:11 +00:00
|
|
|
$modelFileName = $trainingSetFileName.'-model';
|
|
|
|
|
2016-05-07 20:17:12 +00:00
|
|
|
$command = $this->buildTrainCommand($trainingSetFileName, $modelFileName);
|
2016-05-05 21:29:11 +00:00
|
|
|
$output = '';
|
|
|
|
exec(escapeshellcmd($command), $output);
|
|
|
|
|
|
|
|
$this->model = file_get_contents($modelFileName);
|
|
|
|
|
|
|
|
unlink($trainingSetFileName);
|
|
|
|
unlink($modelFileName);
|
|
|
|
}
|
|
|
|
|
2017-11-06 07:56:37 +00:00
|
|
|
public function getModel(): string
|
2016-05-05 21:29:11 +00:00
|
|
|
{
|
|
|
|
return $this->model;
|
|
|
|
}
|
2016-05-06 20:33:04 +00:00
|
|
|
|
2016-05-06 20:55:41 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-05-06 20:33:04 +00:00
|
|
|
public function predict(array $samples)
|
|
|
|
{
|
2016-05-06 20:55:41 +00:00
|
|
|
$testSet = DataTransformer::testSet($samples);
|
2016-12-06 07:50:18 +00:00
|
|
|
file_put_contents($testSetFileName = $this->varPath.uniqid('phpml', true), $testSet);
|
2016-05-07 20:17:12 +00:00
|
|
|
file_put_contents($modelFileName = $testSetFileName.'-model', $this->model);
|
2016-05-06 20:55:41 +00:00
|
|
|
$outputFileName = $testSetFileName.'-output';
|
|
|
|
|
2016-05-07 09:22:37 +00:00
|
|
|
$command = sprintf('%ssvm-predict%s %s %s %s', $this->binPath, $this->getOSExtension(), $testSetFileName, $modelFileName, $outputFileName);
|
2016-05-06 20:55:41 +00:00
|
|
|
$output = '';
|
|
|
|
exec(escapeshellcmd($command), $output);
|
|
|
|
|
2016-05-07 21:04:58 +00:00
|
|
|
$predictions = file_get_contents($outputFileName);
|
2016-05-06 20:55:41 +00:00
|
|
|
|
|
|
|
unlink($testSetFileName);
|
|
|
|
unlink($modelFileName);
|
|
|
|
unlink($outputFileName);
|
|
|
|
|
2016-05-07 21:04:58 +00:00
|
|
|
if (in_array($this->type, [Type::C_SVC, Type::NU_SVC])) {
|
2017-03-17 10:44:45 +00:00
|
|
|
$predictions = DataTransformer::predictions($predictions, $this->targets);
|
2016-05-07 21:04:58 +00:00
|
|
|
} else {
|
|
|
|
$predictions = explode(PHP_EOL, trim($predictions));
|
|
|
|
}
|
2016-05-07 20:17:12 +00:00
|
|
|
|
|
|
|
if (!is_array($samples[0])) {
|
|
|
|
return $predictions[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $predictions;
|
2016-05-06 20:33:04 +00:00
|
|
|
}
|
2016-05-07 09:22:37 +00:00
|
|
|
|
2017-11-06 07:56:37 +00:00
|
|
|
private function getOSExtension(): string
|
2016-05-07 09:22:37 +00:00
|
|
|
{
|
2016-07-01 20:25:57 +00:00
|
|
|
$os = strtoupper(substr(PHP_OS, 0, 3));
|
|
|
|
if ($os === 'WIN') {
|
2016-05-07 09:22:37 +00:00
|
|
|
return '.exe';
|
2016-07-01 20:25:57 +00:00
|
|
|
} elseif ($os === 'DAR') {
|
|
|
|
return '-osx';
|
2016-05-07 09:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
2016-05-07 20:17:12 +00:00
|
|
|
|
|
|
|
private function buildTrainCommand(string $trainingSetFileName, string $modelFileName): string
|
|
|
|
{
|
2017-07-26 06:24:47 +00:00
|
|
|
return sprintf(
|
|
|
|
'%ssvm-train%s -s %s -t %s -c %s -n %s -d %s%s -r %s -p %s -m %s -e %s -h %d -b %d %s %s',
|
2016-05-07 20:17:12 +00:00
|
|
|
$this->binPath,
|
|
|
|
$this->getOSExtension(),
|
|
|
|
$this->type,
|
|
|
|
$this->kernel,
|
|
|
|
$this->cost,
|
|
|
|
$this->nu,
|
|
|
|
$this->degree,
|
|
|
|
$this->gamma !== null ? ' -g '.$this->gamma : '',
|
|
|
|
$this->coef0,
|
|
|
|
$this->epsilon,
|
|
|
|
$this->cacheSize,
|
|
|
|
$this->tolerance,
|
|
|
|
$this->shrinking,
|
|
|
|
$this->probabilityEstimates,
|
2016-07-04 20:22:22 +00:00
|
|
|
escapeshellarg($trainingSetFileName),
|
|
|
|
escapeshellarg($modelFileName)
|
2016-05-07 20:17:12 +00:00
|
|
|
);
|
|
|
|
}
|
2017-09-02 20:44:19 +00:00
|
|
|
|
|
|
|
private function ensureDirectorySeparator(string &$path)
|
|
|
|
{
|
|
|
|
if (substr($path, -1) !== DIRECTORY_SEPARATOR) {
|
|
|
|
$path .= DIRECTORY_SEPARATOR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function verifyBinPath(string $path)
|
|
|
|
{
|
|
|
|
if (!is_dir($path)) {
|
|
|
|
throw InvalidArgumentException::pathNotFound($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
$osExtension = $this->getOSExtension();
|
|
|
|
foreach (['svm-predict', 'svm-scale', 'svm-train'] as $filename) {
|
|
|
|
$filePath = $path.$filename.$osExtension;
|
|
|
|
if (!file_exists($filePath)) {
|
|
|
|
throw InvalidArgumentException::fileNotFound($filePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_executable($filePath)) {
|
|
|
|
throw InvalidArgumentException::fileNotExecutable($filePath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-05 21:29:11 +00:00
|
|
|
}
|