mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-11-26 06:46:45 +00:00
726cf4cddf
* travis: move coveralls here, decouple from package * composer: use PSR4 * phpunit: simpler config * travis: add ecs run * composer: add ecs dev * use standard vendor/bin directory for dependency bins, confuses with local bins and require gitignore handling * ecs: add PSR2 * [cs] PSR2 spacing fixes * [cs] PSR2 class name fix * [cs] PHP7 fixes - return semicolon spaces, old rand functions, typehints * [cs] fix less strict typehints * fix typehints to make tests pass * ecs: ignore typehint-less elements * [cs] standardize arrays * [cs] standardize docblock, remove unused comments * [cs] use self where possible * [cs] sort class elements, from public to private * [cs] do not use yoda (found less yoda-cases, than non-yoda) * space * [cs] do not assign in condition * [cs] use namespace imports if possible * [cs] use ::class over strings * [cs] fix defaults for arrays properties, properties and constants single spacing * cleanup ecs comments * [cs] use item per line in multi-items array * missing line * misc * rebase
108 lines
2.6 KiB
PHP
108 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace tests;
|
|
|
|
use Phpml\Classification\SVC;
|
|
use Phpml\FeatureExtraction\TfIdfTransformer;
|
|
use Phpml\FeatureExtraction\TokenCountVectorizer;
|
|
use Phpml\Pipeline;
|
|
use Phpml\Preprocessing\Imputer;
|
|
use Phpml\Preprocessing\Imputer\Strategy\MostFrequentStrategy;
|
|
use Phpml\Preprocessing\Normalizer;
|
|
use Phpml\Regression\SVR;
|
|
use Phpml\Tokenization\WordTokenizer;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class PipelineTest extends TestCase
|
|
{
|
|
public function testPipelineConstruction(): void
|
|
{
|
|
$transformers = [
|
|
new TfIdfTransformer(),
|
|
];
|
|
$estimator = new SVC();
|
|
|
|
$pipeline = new Pipeline($transformers, $estimator);
|
|
|
|
$this->assertEquals($transformers, $pipeline->getTransformers());
|
|
$this->assertEquals($estimator, $pipeline->getEstimator());
|
|
}
|
|
|
|
public function testPipelineEstimatorSetter(): void
|
|
{
|
|
$pipeline = new Pipeline([new TfIdfTransformer()], new SVC());
|
|
|
|
$estimator = new SVR();
|
|
$pipeline->setEstimator($estimator);
|
|
|
|
$this->assertEquals($estimator, $pipeline->getEstimator());
|
|
}
|
|
|
|
public function testPipelineWorkflow(): void
|
|
{
|
|
$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]);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|