fix Pipeline transformation

This commit is contained in:
Arkadiusz Kondas 2016-06-16 10:26:29 +02:00
parent d21a401365
commit 26f2cbabc4
2 changed files with 33 additions and 1 deletions

View File

@ -68,7 +68,7 @@ class Pipeline implements Estimator
public function train(array $samples, array $targets)
{
foreach ($this->transformers as $transformer) {
$samples = $transformer->transform($samples);
$transformer->transform($samples);
}
$this->estimator->train($samples, $targets);

View File

@ -7,6 +7,9 @@ namespace tests;
use Phpml\Classification\SVC;
use Phpml\FeatureExtraction\TfIdfTransformer;
use Phpml\Pipeline;
use Phpml\Preprocessing\Imputer;
use Phpml\Preprocessing\Normalizer;
use Phpml\Preprocessing\Imputer\Strategy\MostFrequentStrategy;
class PipelineTest extends \PHPUnit_Framework_TestCase
{
@ -22,4 +25,33 @@ class PipelineTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($transformers, $pipeline->getTransformers());
$this->assertEquals($estimator, $pipeline->getEstimator());
}
public function testPipelineWorkflow()
{
$transformers = [
new Imputer(null, new MostFrequentStrategy()),
new Normalizer(),
];
$estimator = new SVC();
$samples = [
[1, -1, 2],
[2, 0, null],
[null, 1, -1],
];
$targets = [
4,
1,
4
];
$pipeline = new Pipeline($transformers, $estimator);
$pipeline->train($samples, $targets);
$predicted = $pipeline->predict([[0, 0, 0]]);
$this->assertEquals(4, $predicted[0]);
}
}