2016-05-05 21:29:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types = 1);
|
|
|
|
|
|
|
|
namespace Phpml\SupportVectorMachine;
|
|
|
|
|
|
|
|
class SupportVectorMachine
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
private $labels;
|
|
|
|
|
2016-05-05 21:29:11 +00:00
|
|
|
/**
|
2016-05-07 20:17:12 +00:00
|
|
|
* @param int $type
|
|
|
|
* @param int $kernel
|
|
|
|
* @param float $cost
|
|
|
|
* @param float $nu
|
|
|
|
* @param int $degree
|
|
|
|
* @param float|null $gamma
|
|
|
|
* @param float $coef0
|
|
|
|
* @param float $epsilon
|
|
|
|
* @param float $tolerance
|
|
|
|
* @param int $cacheSize
|
|
|
|
* @param bool $shrinking
|
|
|
|
* @param bool $probabilityEstimates
|
2016-05-05 21:29:11 +00:00
|
|
|
*/
|
2016-05-07 20:17:12 +00:00
|
|
|
public function __construct(
|
|
|
|
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-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
|
|
|
|
|
|
|
$rootPath = realpath(implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), '..', '..', '..'])).DIRECTORY_SEPARATOR;
|
|
|
|
|
|
|
|
$this->binPath = $rootPath.'bin'.DIRECTORY_SEPARATOR.'libsvm'.DIRECTORY_SEPARATOR;
|
|
|
|
$this->varPath = $rootPath.'var'.DIRECTORY_SEPARATOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $samples
|
|
|
|
* @param array $labels
|
|
|
|
*/
|
|
|
|
public function train(array $samples, array $labels)
|
|
|
|
{
|
2016-05-06 20:55:41 +00:00
|
|
|
$this->labels = $labels;
|
2016-05-07 21:04:58 +00:00
|
|
|
$trainingSet = DataTransformer::trainingSet($samples, $labels, in_array($this->type, [Type::EPSILON_SVR, Type::NU_SVR]));
|
2016-05-05 21:29:11 +00:00
|
|
|
file_put_contents($trainingSetFileName = $this->varPath.uniqid(), $trainingSet);
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getModel()
|
|
|
|
{
|
|
|
|
return $this->model;
|
|
|
|
}
|
2016-05-06 20:33:04 +00:00
|
|
|
|
2016-05-06 20:55:41 +00:00
|
|
|
/**
|
|
|
|
* @param array $samples
|
|
|
|
*
|
|
|
|
* @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);
|
|
|
|
file_put_contents($testSetFileName = $this->varPath.uniqid(), $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])) {
|
|
|
|
$predictions = DataTransformer::predictions($predictions, $this->labels);
|
|
|
|
} 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
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getOSExtension()
|
|
|
|
{
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $trainingSetFileName
|
|
|
|
* @param $modelFileName
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function buildTrainCommand(string $trainingSetFileName, string $modelFileName): string
|
|
|
|
{
|
|
|
|
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\'',
|
|
|
|
$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,
|
|
|
|
$trainingSetFileName,
|
|
|
|
$modelFileName
|
|
|
|
);
|
|
|
|
}
|
2016-05-05 21:29:11 +00:00
|
|
|
}
|