2016-06-16 07:00:10 +00:00
|
|
|
<?php
|
|
|
|
|
2016-11-20 21:53:17 +00:00
|
|
|
declare(strict_types=1);
|
2016-06-16 07:00:10 +00:00
|
|
|
|
2016-06-16 07:58:12 +00:00
|
|
|
namespace Phpml;
|
2016-06-16 07:00:10 +00:00
|
|
|
|
2019-05-14 19:26:25 +00:00
|
|
|
use Phpml\Exception\InvalidOperationException;
|
|
|
|
|
|
|
|
class Pipeline implements Estimator, Transformer
|
2016-06-16 07:00:10 +00:00
|
|
|
{
|
|
|
|
/**
|
2018-12-12 20:56:44 +00:00
|
|
|
* @var Transformer[]
|
2016-06-16 07:00:10 +00:00
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
private $transformers = [];
|
2016-06-16 07:00:10 +00:00
|
|
|
|
|
|
|
/**
|
2019-05-14 19:26:25 +00:00
|
|
|
* @var Estimator|null
|
2016-06-16 07:00:10 +00:00
|
|
|
*/
|
2016-06-16 07:58:12 +00:00
|
|
|
private $estimator;
|
|
|
|
|
|
|
|
/**
|
2018-12-12 20:56:44 +00:00
|
|
|
* @param Transformer[] $transformers
|
2016-06-16 07:58:12 +00:00
|
|
|
*/
|
2019-05-14 19:26:25 +00:00
|
|
|
public function __construct(array $transformers, ?Estimator $estimator = null)
|
2016-06-16 07:00:10 +00:00
|
|
|
{
|
2019-05-14 19:26:25 +00:00
|
|
|
$this->transformers = array_map(static function (Transformer $transformer): Transformer {
|
|
|
|
return $transformer;
|
|
|
|
}, $transformers);
|
2016-06-16 07:58:12 +00:00
|
|
|
$this->estimator = $estimator;
|
2016-06-16 07:00:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-12-12 20:56:44 +00:00
|
|
|
* @return Transformer[]
|
2016-06-16 07:00:10 +00:00
|
|
|
*/
|
2017-11-22 21:16:10 +00:00
|
|
|
public function getTransformers(): array
|
2016-06-16 07:00:10 +00:00
|
|
|
{
|
2016-06-16 07:58:12 +00:00
|
|
|
return $this->transformers;
|
2016-06-16 07:00:10 +00:00
|
|
|
}
|
2016-06-16 07:58:12 +00:00
|
|
|
|
2019-05-14 19:26:25 +00:00
|
|
|
public function getEstimator(): ?Estimator
|
2016-06-16 07:58:12 +00:00
|
|
|
{
|
|
|
|
return $this->estimator;
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function train(array $samples, array $targets): void
|
2016-06-16 07:58:12 +00:00
|
|
|
{
|
2019-05-14 19:26:25 +00:00
|
|
|
if ($this->estimator === null) {
|
|
|
|
throw new InvalidOperationException('Pipeline without estimator can\'t use train method');
|
|
|
|
}
|
|
|
|
|
2017-05-24 07:06:54 +00:00
|
|
|
foreach ($this->transformers as $transformer) {
|
2018-02-14 18:05:48 +00:00
|
|
|
$transformer->fit($samples, $targets);
|
2019-05-14 19:26:25 +00:00
|
|
|
$transformer->transform($samples, $targets);
|
2017-05-24 07:06:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 07:58:12 +00:00
|
|
|
$this->estimator->train($samples, $targets);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function predict(array $samples)
|
|
|
|
{
|
2019-05-14 19:26:25 +00:00
|
|
|
if ($this->estimator === null) {
|
|
|
|
throw new InvalidOperationException('Pipeline without estimator can\'t use predict method');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->transform($samples);
|
2016-06-16 14:10:46 +00:00
|
|
|
|
2016-06-16 07:58:12 +00:00
|
|
|
return $this->estimator->predict($samples);
|
|
|
|
}
|
2016-06-16 14:10:46 +00:00
|
|
|
|
2019-05-14 19:26:25 +00:00
|
|
|
public function fit(array $samples, ?array $targets = null): void
|
|
|
|
{
|
|
|
|
foreach ($this->transformers as $transformer) {
|
|
|
|
$transformer->fit($samples, $targets);
|
|
|
|
$transformer->transform($samples, $targets);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function transform(array &$samples, ?array &$targets = null): void
|
2016-06-16 14:10:46 +00:00
|
|
|
{
|
|
|
|
foreach ($this->transformers as $transformer) {
|
2019-05-14 19:26:25 +00:00
|
|
|
$transformer->transform($samples, $targets);
|
2016-06-16 14:10:46 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-16 07:00:10 +00:00
|
|
|
}
|