1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare (strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Phpml\SupportVectorMachine; |
6
|
|
|
|
7
|
|
|
class DataTransformer |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param array $samples |
11
|
|
|
* @param array $labels |
12
|
|
|
* @param bool $targets |
13
|
|
|
* |
14
|
|
|
* @return string |
15
|
|
|
*/ |
16
|
|
|
public static function trainingSet(array $samples, array $labels, bool $targets = false): string |
17
|
|
|
{ |
18
|
|
|
$set = ''; |
19
|
|
|
if (!$targets) { |
20
|
|
|
$numericLabels = self::numericLabels($labels); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
foreach ($labels as $index => $label) { |
24
|
|
|
$set .= sprintf('%s %s %s', ($targets ? $label : $numericLabels[$label]), self::sampleRow($samples[$index]), PHP_EOL); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return $set; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $samples |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
public static function testSet(array $samples): string |
36
|
|
|
{ |
37
|
|
|
if (!is_array($samples[0])) { |
38
|
|
|
$samples = [$samples]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$set = ''; |
42
|
|
|
foreach ($samples as $sample) { |
43
|
|
|
$set .= sprintf('0 %s %s', self::sampleRow($sample), PHP_EOL); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $set; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $rawPredictions |
51
|
|
|
* @param array $labels |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public static function predictions(string $rawPredictions, array $labels): array |
56
|
|
|
{ |
57
|
|
|
$numericLabels = self::numericLabels($labels); |
58
|
|
|
$results = []; |
59
|
|
|
foreach (explode(PHP_EOL, $rawPredictions) as $result) { |
60
|
|
|
if (strlen($result) > 0) { |
61
|
|
|
$results[] = array_search($result, $numericLabels); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $results; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param array $labels |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public static function numericLabels(array $labels): array |
74
|
|
|
{ |
75
|
|
|
$numericLabels = []; |
76
|
|
|
foreach ($labels as $label) { |
77
|
|
|
if (isset($numericLabels[$label])) { |
78
|
|
|
continue; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$numericLabels[$label] = count($numericLabels); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $numericLabels; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array $sample |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
private static function sampleRow(array $sample): string |
93
|
|
|
{ |
94
|
|
|
$row = []; |
95
|
|
|
foreach ($sample as $index => $feature) { |
96
|
|
|
$row[] = sprintf('%s:%s', $index + 1, $feature); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return implode(' ', $row); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: