1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare (strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Phpml\SupportVectorMachine; |
6
|
|
|
|
7
|
|
|
class SupportVectorMachine |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var int |
11
|
|
|
*/ |
12
|
|
|
private $type; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var int |
16
|
|
|
*/ |
17
|
|
|
private $kernel; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var float |
21
|
|
|
*/ |
22
|
|
|
private $cost; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $binPath; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $varPath; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $model; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private $labels; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param int $type |
46
|
|
|
* @param int $kernel |
47
|
|
|
* @param float $cost |
48
|
|
|
*/ |
49
|
|
|
public function __construct(int $type, int $kernel, float $cost) |
50
|
|
|
{ |
51
|
|
|
$this->type = $type; |
52
|
|
|
$this->kernel = $kernel; |
53
|
|
|
$this->cost = $cost; |
54
|
|
|
|
55
|
|
|
$rootPath = realpath(implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), '..', '..', '..'])).DIRECTORY_SEPARATOR; |
56
|
|
|
|
57
|
|
|
$this->binPath = $rootPath.'bin'.DIRECTORY_SEPARATOR.'libsvm'.DIRECTORY_SEPARATOR; |
58
|
|
|
$this->varPath = $rootPath.'var'.DIRECTORY_SEPARATOR; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param array $samples |
63
|
|
|
* @param array $labels |
64
|
|
|
*/ |
65
|
|
|
public function train(array $samples, array $labels) |
66
|
|
|
{ |
67
|
|
|
$this->labels = $labels; |
68
|
|
|
$trainingSet = DataTransformer::trainingSet($samples, $labels); |
69
|
|
|
file_put_contents($trainingSetFileName = $this->varPath.uniqid(), $trainingSet); |
70
|
|
|
$modelFileName = $trainingSetFileName.'-model'; |
71
|
|
|
|
72
|
|
|
$command = sprintf('%ssvm-train -s %s -t %s -c %s %s %s', $this->binPath, $this->type, $this->kernel, $this->cost, $trainingSetFileName, $modelFileName); |
73
|
|
|
$output = ''; |
74
|
|
|
exec(escapeshellcmd($command), $output); |
75
|
|
|
|
76
|
|
|
$this->model = file_get_contents($modelFileName); |
77
|
|
|
|
78
|
|
|
unlink($trainingSetFileName); |
79
|
|
|
unlink($modelFileName); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function getModel() |
86
|
|
|
{ |
87
|
|
|
return $this->model; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param array $samples |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function predict(array $samples) |
96
|
|
|
{ |
97
|
|
|
$testSet = DataTransformer::testSet($samples); |
98
|
|
|
file_put_contents($testSetFileName = $this->varPath.uniqid(), $testSet); |
99
|
|
|
$modelFileName = $testSetFileName.'-model'; |
100
|
|
|
file_put_contents($modelFileName, $this->model); |
101
|
|
|
$outputFileName = $testSetFileName.'-output'; |
102
|
|
|
|
103
|
|
|
$command = sprintf('%ssvm-predict %s %s %s', $this->binPath, $testSetFileName, $modelFileName, $outputFileName); |
104
|
|
|
$output = ''; |
105
|
|
|
exec(escapeshellcmd($command), $output); |
106
|
|
|
|
107
|
|
|
$predictions = file_get_contents($outputFileName); |
108
|
|
|
|
109
|
|
|
unlink($testSetFileName); |
110
|
|
|
unlink($modelFileName); |
111
|
|
|
unlink($outputFileName); |
112
|
|
|
|
113
|
|
|
return DataTransformer::results($predictions, $this->labels); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|