php-ml/tests/Phpml/PipelineTest.php

108 lines
2.6 KiB
PHP
Raw Normal View History

2016-06-16 07:58:17 +00:00
<?php
2016-11-20 21:53:17 +00:00
declare(strict_types=1);
2016-06-16 07:58:17 +00:00
namespace tests;
use Phpml\Classification\SVC;
use Phpml\FeatureExtraction\TfIdfTransformer;
use Phpml\FeatureExtraction\TokenCountVectorizer;
2016-06-16 07:58:17 +00:00
use Phpml\Pipeline;
2016-06-16 08:26:29 +00:00
use Phpml\Preprocessing\Imputer;
use Phpml\Preprocessing\Imputer\Strategy\MostFrequentStrategy;
use Phpml\Preprocessing\Normalizer;
2016-07-24 11:52:52 +00:00
use Phpml\Regression\SVR;
use Phpml\Tokenization\WordTokenizer;
2017-02-03 11:58:25 +00:00
use PHPUnit\Framework\TestCase;
2016-06-16 07:58:17 +00:00
2017-02-03 11:58:25 +00:00
class PipelineTest extends TestCase
2016-06-16 07:58:17 +00:00
{
public function testPipelineConstruction(): void
2016-06-16 07:58:17 +00:00
{
$transformers = [
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 testPipelineEstimatorSetter(): void
2016-07-24 11:52:52 +00:00
{
$pipeline = new Pipeline([new TfIdfTransformer()], new SVC());
$estimator = new SVR();
$pipeline->setEstimator($estimator);
$this->assertEquals($estimator, $pipeline->getEstimator());
}
public function testPipelineWorkflow(): void
2016-06-16 08:26:29 +00:00
{
$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,
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]);
}
public function testPipelineTransformers(): void
{
$transformers = [
new TokenCountVectorizer(new WordTokenizer()),
new TfIdfTransformer()
];
$estimator = new SVC();
$samples = [
'Hello Paul',
'Hello Martin',
'Goodbye Tom',
'Hello John',
'Goodbye Alex',
'Bye Tony',
];
$targets = [
'greetings',
'greetings',
'farewell',
'greetings',
'farewell',
'farewell',
];
$pipeline = new Pipeline($transformers, $estimator);
$pipeline->train($samples, $targets);
$expected = ['greetings', 'farewell'];
$predicted = $pipeline->predict(['Hello Max', 'Goodbye Mark']);
$this->assertEquals($expected, $predicted);
}
2016-06-16 07:58:17 +00:00
}