mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2025-01-24 07:38:24 +00:00
fix Pipeline transformation
This commit is contained in:
parent
d21a401365
commit
26f2cbabc4
@ -68,7 +68,7 @@ class Pipeline implements Estimator
|
|||||||
public function train(array $samples, array $targets)
|
public function train(array $samples, array $targets)
|
||||||
{
|
{
|
||||||
foreach ($this->transformers as $transformer) {
|
foreach ($this->transformers as $transformer) {
|
||||||
$samples = $transformer->transform($samples);
|
$transformer->transform($samples);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->estimator->train($samples, $targets);
|
$this->estimator->train($samples, $targets);
|
||||||
|
@ -7,6 +7,9 @@ namespace tests;
|
|||||||
use Phpml\Classification\SVC;
|
use Phpml\Classification\SVC;
|
||||||
use Phpml\FeatureExtraction\TfIdfTransformer;
|
use Phpml\FeatureExtraction\TfIdfTransformer;
|
||||||
use Phpml\Pipeline;
|
use Phpml\Pipeline;
|
||||||
|
use Phpml\Preprocessing\Imputer;
|
||||||
|
use Phpml\Preprocessing\Normalizer;
|
||||||
|
use Phpml\Preprocessing\Imputer\Strategy\MostFrequentStrategy;
|
||||||
|
|
||||||
class PipelineTest extends \PHPUnit_Framework_TestCase
|
class PipelineTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
@ -22,4 +25,33 @@ class PipelineTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals($transformers, $pipeline->getTransformers());
|
$this->assertEquals($transformers, $pipeline->getTransformers());
|
||||||
$this->assertEquals($estimator, $pipeline->getEstimator());
|
$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]);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user