Skip to content

Commit

Permalink
simple pipeline test
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas committed Jun 16, 2016
1 parent cab79e7 commit 374182a
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/Phpml/Classification/KNearestNeighbors.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(int $k = 3, Distance $distanceMetric = null)

$this->k = $k;
$this->samples = [];
$this->labels = [];
$this->targets = [];
$this->distanceMetric = $distanceMetric;
}

Expand All @@ -48,10 +48,10 @@ protected function predictSample(array $sample)
{
$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);
Expand Down
2 changes: 1 addition & 1 deletion src/Phpml/Classification/NaiveBayes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down
8 changes: 4 additions & 4 deletions src/Phpml/Helper/Trainable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
77 changes: 67 additions & 10 deletions src/Phpml/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
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)
{
$this->stages = $stages;
$this->estimator = $estimator;
}

/**
* @param mixed $stage
* @return array|Transformer[]
*/
public function addStage($stage)
public function getTransformers()
{
$this->stages[] = $stage;
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);
}

}

0 comments on commit 374182a

Please sign in to comment.