-
-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathPipeline.php
99 lines (84 loc) · 2.24 KB
/
Pipeline.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace Rubix\Engine;
use Rubix\Engine\Preprocessors\Preprocessor;
use RuntimeException;
class Pipeline implements Estimator
{
/**
* The estimator.
*
* @var \Rubix\Engine\Estimator
*/
protected $estimator;
/**
* The transformers that process the sample data before they are fed to the
* estimator for training, testing, and prediction.
*
* @var array
*/
protected $preprocessors = [
//
];
/**
* @param \Rubix\Engine\Estimator $estimator
* @param array $preprocessors
* @return void
*/
public function __construct(Estimator $estimator, array $preprocessors = [])
{
foreach ($preprocessors as $preprocessor) {
$this->addPreprocessor($preprocessor);
}
$this->estimator = $estimator;
}
/**
* Return the instance of the estimator.
*
* @return \Rubix\Engine\Estimator
*/
public function estimator() : Estimator
{
return $this->estimator;
}
/**
* Run the training dataset through all preprocessors in order and use the
* transformed dataset to train the estimator.
*
* @param \Rubix\Engine\Dataset $data
* @throws \RuntimeException
* @return void
*/
public function train(Dataset $data) : void
{
foreach ($this->preprocessors as $preprocessor) {
$preprocessor->fit($data);
$data->transform($preprocessor);
}
$this->estimator->train($data);
}
/**
* Preprocess the sample and make a prediction.
*
* @param array $sample
* @return \Rubix\Engine\Prediction
*/
public function predict(array $sample) : Prediction
{
$samples = [$sample];
foreach ($this->preprocessors as $preprocessor) {
$preprocessor->transform($samples);
}
return $this->estimator->predict($samples[0]);
}
/**
* Add a preprocessor middleware to the pipeline.
*
* @param \Rubix\Engine\Preprocessors\Preprocessor $preprocessor
* @return self
*/
public function addPreprocessor(Preprocessor $preprocessor) : self
{
$this->preprocessors[] = $preprocessor;
return $this;
}
}