2016-06-16 07:00:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types = 1);
|
|
|
|
|
2016-06-16 07:58:12 +00:00
|
|
|
namespace Phpml;
|
2016-06-16 07:00:10 +00:00
|
|
|
|
2016-06-16 07:58:12 +00:00
|
|
|
class Pipeline implements Estimator
|
2016-06-16 07:00:10 +00:00
|
|
|
{
|
|
|
|
/**
|
2016-06-16 07:58:12 +00:00
|
|
|
* @var array|Transformer[]
|
2016-06-16 07:00:10 +00:00
|
|
|
*/
|
2016-06-16 07:58:12 +00:00
|
|
|
private $transformers;
|
2016-06-16 07:00:10 +00:00
|
|
|
|
|
|
|
/**
|
2016-06-16 07:58:12 +00:00
|
|
|
* @var Estimator
|
2016-06-16 07:00:10 +00:00
|
|
|
*/
|
2016-06-16 07:58:12 +00:00
|
|
|
private $estimator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array|Transformer[] $transformers
|
2016-06-16 08:01:40 +00:00
|
|
|
* @param Estimator $estimator
|
2016-06-16 07:58:12 +00:00
|
|
|
*/
|
|
|
|
public function __construct(array $transformers = [], Estimator $estimator)
|
|
|
|
{
|
|
|
|
foreach ($transformers as $transformer) {
|
|
|
|
$this->addTransformer($transformer);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->estimator = $estimator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Transformer $transformer
|
|
|
|
*/
|
|
|
|
public function addTransformer(Transformer $transformer)
|
|
|
|
{
|
|
|
|
$this->transformers[] = $transformer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Estimator $estimator
|
|
|
|
*/
|
|
|
|
public function setEstimator(Estimator $estimator)
|
2016-06-16 07:00:10 +00:00
|
|
|
{
|
2016-06-16 07:58:12 +00:00
|
|
|
$this->estimator = $estimator;
|
2016-06-16 07:00:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-16 07:58:12 +00:00
|
|
|
* @return array|Transformer[]
|
2016-06-16 07:00:10 +00:00
|
|
|
*/
|
2016-06-16 07:58:12 +00:00
|
|
|
public function getTransformers()
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Estimator
|
|
|
|
*/
|
|
|
|
public function getEstimator()
|
|
|
|
{
|
|
|
|
return $this->estimator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $samples
|
|
|
|
* @param array $targets
|
|
|
|
*/
|
|
|
|
public function train(array $samples, array $targets)
|
|
|
|
{
|
|
|
|
foreach ($this->transformers as $transformer) {
|
2016-06-16 08:26:29 +00:00
|
|
|
$transformer->transform($samples);
|
2016-06-16 07:58:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->estimator->train($samples, $targets);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $samples
|
2016-06-16 08:01:40 +00:00
|
|
|
*
|
2016-06-16 07:58:12 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function predict(array $samples)
|
|
|
|
{
|
|
|
|
return $this->estimator->predict($samples);
|
|
|
|
}
|
2016-06-16 07:00:10 +00:00
|
|
|
}
|