2016-06-16 07:58:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types = 1);
|
|
|
|
|
|
|
|
namespace tests;
|
|
|
|
|
|
|
|
use Phpml\Classification\SVC;
|
|
|
|
use Phpml\FeatureExtraction\TfIdfTransformer;
|
|
|
|
use Phpml\Pipeline;
|
2016-06-16 08:26:29 +00:00
|
|
|
use Phpml\Preprocessing\Imputer;
|
|
|
|
use Phpml\Preprocessing\Normalizer;
|
|
|
|
use Phpml\Preprocessing\Imputer\Strategy\MostFrequentStrategy;
|
2016-06-16 07:58:17 +00:00
|
|
|
|
|
|
|
class PipelineTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
public function testPipelineConstruction()
|
|
|
|
{
|
|
|
|
$transformers = [
|
2016-06-16 08:01:40 +00:00
|
|
|
new TfIdfTransformer(),
|
2016-06-16 07:58:17 +00:00
|
|
|
];
|
|
|
|
$estimator = new SVC();
|
|
|
|
|
|
|
|
$pipeline = new Pipeline($transformers, $estimator);
|
|
|
|
|
|
|
|
$this->assertEquals($transformers, $pipeline->getTransformers());
|
|
|
|
$this->assertEquals($estimator, $pipeline->getEstimator());
|
|
|
|
}
|
2016-06-16 08:26:29 +00:00
|
|
|
|
|
|
|
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,
|
2016-06-16 14:10:46 +00:00
|
|
|
4,
|
2016-06-16 08:26:29 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
$pipeline = new Pipeline($transformers, $estimator);
|
|
|
|
$pipeline->train($samples, $targets);
|
|
|
|
|
|
|
|
$predicted = $pipeline->predict([[0, 0, 0]]);
|
|
|
|
|
|
|
|
$this->assertEquals(4, $predicted[0]);
|
|
|
|
}
|
2016-06-16 07:58:17 +00:00
|
|
|
}
|