simple pipeline test

This commit is contained in:
Arkadiusz Kondas 2016-06-16 09:58:12 +02:00
parent cab79e7e36
commit 374182a6d4
4 changed files with 75 additions and 18 deletions

View File

@ -35,7 +35,7 @@ class KNearestNeighbors implements Classifier
$this->k = $k;
$this->samples = [];
$this->labels = [];
$this->targets = [];
$this->distanceMetric = $distanceMetric;
}
@ -48,10 +48,10 @@ class KNearestNeighbors implements Classifier
{
$distances = $this->kNeighborsDistances($sample);
$predictions = array_combine(array_values($this->labels), array_fill(0, count($this->labels), 0));
$predictions = array_combine(array_values($this->targets), array_fill(0, count($this->targets), 0));
foreach ($distances as $index => $distance) {
++$predictions[$this->labels[$index]];
++$predictions[$this->targets[$index]];
}
arsort($predictions);

View File

@ -19,7 +19,7 @@ class NaiveBayes implements Classifier
protected function predictSample(array $sample)
{
$predictions = [];
foreach ($this->labels as $index => $label) {
foreach ($this->targets as $index => $label) {
$predictions[$label] = 0;
foreach ($sample as $token => $count) {
if (array_key_exists($token, $this->samples[$index])) {

View File

@ -14,15 +14,15 @@ trait Trainable
/**
* @var array
*/
private $labels;
private $targets;
/**
* @param array $samples
* @param array $labels
* @param array $targets
*/
public function train(array $samples, array $labels)
public function train(array $samples, array $targets)
{
$this->samples = $samples;
$this->labels = $labels;
$this->targets = $targets;
}
}

View File

@ -2,28 +2,85 @@
declare (strict_types = 1);
namespace Phpml\Pipeline;
namespace Phpml;
class Pipeline
class Pipeline implements Estimator
{
/**
* @var array
* @var array|Transformer[]
*/
private $stages;
private $transformers;
/**
* @param array $stages
* @var Estimator
*/
public function __construct(array $stages)
private $estimator;
/**
* @param array|Transformer[] $transformers
* @param Estimator $estimator
*/
public function __construct(array $transformers = [], Estimator $estimator)
{
$this->stages = $stages;
foreach ($transformers as $transformer) {
$this->addTransformer($transformer);
}
$this->estimator = $estimator;
}
/**
* @param mixed $stage
* @param Transformer $transformer
*/
public function addStage($stage)
public function addTransformer(Transformer $transformer)
{
$this->stages[] = $stage;
$this->transformers[] = $transformer;
}
/**
* @param Estimator $estimator
*/
public function setEstimator(Estimator $estimator)
{
$this->estimator = $estimator;
}
/**
* @return array|Transformer[]
*/
public function getTransformers()
{
return $this->transformers;
}
/**
* @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) {
$samples = $transformer->transform($samples);
}
$this->estimator->train($samples, $targets);
}
/**
* @param array $samples
* @return mixed
*/
public function predict(array $samples)
{
return $this->estimator->predict($samples);
}
}