2016-06-16 09:00:10 +02:00
|
|
|
<?php
|
|
|
|
|
2016-11-20 22:53:17 +01:00
|
|
|
declare(strict_types=1);
|
2016-06-16 09:00:10 +02:00
|
|
|
|
2016-06-16 09:58:12 +02:00
|
|
|
namespace Phpml;
|
2016-06-16 09:00:10 +02:00
|
|
|
|
2016-06-16 09:58:12 +02:00
|
|
|
class Pipeline implements Estimator
|
2016-06-16 09:00:10 +02:00
|
|
|
{
|
|
|
|
/**
|
2016-06-16 09:58:12 +02:00
|
|
|
* @var array|Transformer[]
|
2016-06-16 09:00:10 +02:00
|
|
|
*/
|
2017-11-22 22:16:10 +01:00
|
|
|
private $transformers = [];
|
2016-06-16 09:00:10 +02:00
|
|
|
|
|
|
|
/**
|
2016-06-16 09:58:12 +02:00
|
|
|
* @var Estimator
|
2016-06-16 09:00:10 +02:00
|
|
|
*/
|
2016-06-16 09:58:12 +02:00
|
|
|
private $estimator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array|Transformer[] $transformers
|
|
|
|
*/
|
2016-11-20 22:53:17 +01:00
|
|
|
public function __construct(array $transformers, Estimator $estimator)
|
2016-06-16 09:58:12 +02:00
|
|
|
{
|
|
|
|
foreach ($transformers as $transformer) {
|
|
|
|
$this->addTransformer($transformer);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->estimator = $estimator;
|
|
|
|
}
|
|
|
|
|
2017-11-14 21:21:23 +01:00
|
|
|
public function addTransformer(Transformer $transformer): void
|
2016-06-16 09:58:12 +02:00
|
|
|
{
|
|
|
|
$this->transformers[] = $transformer;
|
|
|
|
}
|
|
|
|
|
2017-11-14 21:21:23 +01:00
|
|
|
public function setEstimator(Estimator $estimator): void
|
2016-06-16 09:00:10 +02:00
|
|
|
{
|
2016-06-16 09:58:12 +02:00
|
|
|
$this->estimator = $estimator;
|
2016-06-16 09:00:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-16 09:58:12 +02:00
|
|
|
* @return array|Transformer[]
|
2016-06-16 09:00:10 +02:00
|
|
|
*/
|
2017-11-22 22:16:10 +01:00
|
|
|
public function getTransformers(): array
|
2016-06-16 09:00:10 +02:00
|
|
|
{
|
2016-06-16 09:58:12 +02:00
|
|
|
return $this->transformers;
|
2016-06-16 09:00:10 +02:00
|
|
|
}
|
2016-06-16 09:58:12 +02:00
|
|
|
|
2017-11-06 08:56:37 +01:00
|
|
|
public function getEstimator(): Estimator
|
2016-06-16 09:58:12 +02:00
|
|
|
{
|
|
|
|
return $this->estimator;
|
|
|
|
}
|
|
|
|
|
2017-11-14 21:21:23 +01:00
|
|
|
public function train(array $samples, array $targets): void
|
2016-06-16 09:58:12 +02:00
|
|
|
{
|
2017-05-24 09:06:54 +02:00
|
|
|
foreach ($this->transformers as $transformer) {
|
|
|
|
$transformer->fit($samples);
|
|
|
|
$transformer->transform($samples);
|
|
|
|
}
|
|
|
|
|
2016-06-16 09:58:12 +02:00
|
|
|
$this->estimator->train($samples, $targets);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function predict(array $samples)
|
|
|
|
{
|
2016-06-16 16:10:46 +02:00
|
|
|
$this->transformSamples($samples);
|
|
|
|
|
2016-06-16 09:58:12 +02:00
|
|
|
return $this->estimator->predict($samples);
|
|
|
|
}
|
2016-06-16 16:10:46 +02:00
|
|
|
|
2017-11-14 21:21:23 +01:00
|
|
|
private function transformSamples(array &$samples): void
|
2016-06-16 16:10:46 +02:00
|
|
|
{
|
|
|
|
foreach ($this->transformers as $transformer) {
|
|
|
|
$transformer->transform($samples);
|
|
|
|
}
|
|
|
|
}
|
2016-06-16 09:00:10 +02:00
|
|
|
}
|