php-ml/src/Phpml/Pipeline.php

81 lines
1.6 KiB
PHP
Raw Normal View History

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