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)
|
|
|
|
{
|
2016-06-16 22:16:49 +00:00
|
|
|
$this->fitTransformers($samples);
|
2016-06-16 14:10:46 +00:00
|
|
|
$this->transformSamples($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)
|
|
|
|
{
|
2016-06-16 14:10:46 +00:00
|
|
|
$this->transformSamples($samples);
|
|
|
|
|
2016-06-16 07:58:12 +00:00
|
|
|
return $this->estimator->predict($samples);
|
|
|
|
}
|
2016-06-16 14:10:46 +00:00
|
|
|
|
2016-06-16 22:16:49 +00:00
|
|
|
/**
|
|
|
|
* @param array $samples
|
|
|
|
*/
|
|
|
|
private function fitTransformers(array &$samples)
|
|
|
|
{
|
|
|
|
foreach ($this->transformers as $transformer) {
|
|
|
|
$transformer->fit($samples);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-16 14:10:46 +00:00
|
|
|
/**
|
|
|
|
* @param array $samples
|
|
|
|
*/
|
|
|
|
private function transformSamples(array &$samples)
|
|
|
|
{
|
|
|
|
foreach ($this->transformers as $transformer) {
|
|
|
|
$transformer->transform($samples);
|
|
|
|
}
|
|
|
|
}
|
2016-06-16 07:00:10 +00:00
|
|
|
}
|