1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Phpml\SupportVectorMachine; |
6
|
|
|
|
7
|
|
|
class DataTransformer |
8
|
|
|
{ |
9
|
|
|
public static function trainingSet(array $samples, array $labels, bool $targets = false): string |
10
|
|
|
{ |
11
|
|
|
$set = ''; |
12
|
|
|
$numericLabels = []; |
13
|
|
|
|
14
|
|
|
if (!$targets) { |
15
|
|
|
$numericLabels = self::numericLabels($labels); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
foreach ($labels as $index => $label) { |
19
|
|
|
$set .= sprintf('%s %s %s', ($targets ? $label : $numericLabels[$label]), self::sampleRow($samples[$index]), PHP_EOL); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
return $set; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public static function testSet(array $samples): string |
26
|
|
|
{ |
27
|
|
|
if (!is_array($samples[0])) { |
28
|
|
|
$samples = [$samples]; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$set = ''; |
32
|
|
|
foreach ($samples as $sample) { |
33
|
|
|
$set .= sprintf('0 %s %s', self::sampleRow($sample), PHP_EOL); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $set; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public static function predictions(string $rawPredictions, array $labels): array |
40
|
|
|
{ |
41
|
|
|
$numericLabels = self::numericLabels($labels); |
42
|
|
|
$results = []; |
43
|
|
|
foreach (explode(PHP_EOL, $rawPredictions) as $result) { |
44
|
|
|
if (isset($result[0])) { |
45
|
|
|
$results[] = array_search($result, $numericLabels); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $results; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public static function probabilities(string $rawPredictions, array $labels): array |
53
|
|
|
{ |
54
|
|
|
$numericLabels = self::numericLabels($labels); |
55
|
|
|
|
56
|
|
|
$predictions = explode(PHP_EOL, trim($rawPredictions)); |
57
|
|
|
|
58
|
|
|
$header = array_shift($predictions); |
59
|
|
|
$headerColumns = explode(' ', $header); |
60
|
|
|
array_shift($headerColumns); |
61
|
|
|
|
62
|
|
|
$columnLabels = []; |
63
|
|
|
foreach ($headerColumns as $numericLabel) { |
64
|
|
|
$columnLabels[] = array_search($numericLabel, $numericLabels); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$results = []; |
68
|
|
|
foreach ($predictions as $rawResult) { |
69
|
|
|
$probabilities = explode(' ', $rawResult); |
70
|
|
|
array_shift($probabilities); |
71
|
|
|
|
72
|
|
|
$result = []; |
73
|
|
|
foreach ($probabilities as $i => $prob) { |
74
|
|
|
$result[$columnLabels[$i]] = (float) $prob; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$results[] = $result; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $results; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public static function numericLabels(array $labels): array |
84
|
|
|
{ |
85
|
|
|
$numericLabels = []; |
86
|
|
|
foreach ($labels as $label) { |
87
|
|
|
if (isset($numericLabels[$label])) { |
88
|
|
|
continue; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$numericLabels[$label] = count($numericLabels); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $numericLabels; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private static function sampleRow(array $sample): string |
98
|
|
|
{ |
99
|
|
|
$row = []; |
100
|
|
|
foreach ($sample as $index => $feature) { |
101
|
|
|
$row[] = sprintf('%s:%s', $index + 1, $feature); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return implode(' ', $row); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|