|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare (strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Phpml; |
|
6
|
|
|
|
|
7
|
|
|
class Pipeline implements Estimator |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var array|Transformer[] |
|
11
|
|
|
*/ |
|
12
|
|
|
private $transformers; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var Estimator |
|
16
|
|
|
*/ |
|
17
|
|
|
private $estimator; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param array|Transformer[] $transformers |
|
21
|
|
|
* @param Estimator $estimator |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(array $transformers = [], Estimator $estimator) |
|
24
|
|
|
{ |
|
25
|
|
|
foreach ($transformers as $transformer) { |
|
26
|
|
|
$this->addTransformer($transformer); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$this->estimator = $estimator; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param Transformer $transformer |
|
34
|
|
|
*/ |
|
35
|
|
|
public function addTransformer(Transformer $transformer) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->transformers[] = $transformer; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param Estimator $estimator |
|
42
|
|
|
*/ |
|
43
|
|
|
public function setEstimator(Estimator $estimator) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->estimator = $estimator; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return array|Transformer[] |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getTransformers() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->transformers; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return Estimator |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getEstimator() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->estimator; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param array $samples |
|
66
|
|
|
* @param array $targets |
|
67
|
|
|
*/ |
|
68
|
|
|
public function train(array $samples, array $targets) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->fitTransformers($samples); |
|
71
|
|
|
$this->transformSamples($samples); |
|
72
|
|
|
$this->estimator->train($samples, $targets); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param array $samples |
|
77
|
|
|
* |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
*/ |
|
80
|
|
|
public function predict(array $samples) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->transformSamples($samples); |
|
83
|
|
|
|
|
84
|
|
|
return $this->estimator->predict($samples); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param array $samples |
|
89
|
|
|
*/ |
|
90
|
|
|
private function fitTransformers(array &$samples) |
|
91
|
|
|
{ |
|
92
|
|
|
foreach ($this->transformers as $transformer) { |
|
93
|
|
|
$transformer->fit($samples); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param array $samples |
|
99
|
|
|
*/ |
|
100
|
|
|
private function transformSamples(array &$samples) |
|
101
|
|
|
{ |
|
102
|
|
|
foreach ($this->transformers as $transformer) { |
|
103
|
|
|
$transformer->transform($samples); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|