mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-11-22 13:05:10 +00:00
a348111e97
* tests: update to PHPUnit 6.0 with rector * fix namespaces on tests * composer + tests: use standard test namespace naming * update travis * resolve conflict * phpstan lvl 2 * phpstan lvl 3 * phpstan lvl 4 * phpstan lvl 5 * phpstan lvl 6 * phpstan lvl 7 * level max * resolve conflict * [cs] clean empty docs * composer: bump to PHPUnit 6.4 * cleanup * composer + travis: add phpstan * phpstan lvl 1 * composer: update dev deps * phpstan fixes * update Contributing with new tools * docs: link fixes, PHP version update * composer: drop php-cs-fixer, cs already handled by ecs * ecs: add old set rules * [cs] apply rest of rules
208 lines
6.9 KiB
PHP
208 lines
6.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Phpml\Tests\Association;
|
|
|
|
use Phpml\Association\Apriori;
|
|
use Phpml\ModelManager;
|
|
use PHPUnit\Framework\TestCase;
|
|
use ReflectionClass;
|
|
|
|
class AprioriTest extends TestCase
|
|
{
|
|
private $sampleGreek = [
|
|
['alpha', 'beta', 'epsilon'],
|
|
['alpha', 'beta', 'theta'],
|
|
['alpha', 'beta', 'epsilon'],
|
|
['alpha', 'beta', 'theta'],
|
|
];
|
|
|
|
private $sampleChars = [
|
|
['E', 'D', 'N', 'E+N', 'EN'],
|
|
['E', 'R', 'N', 'E+R', 'E+N', 'ER', 'EN'],
|
|
['D', 'R'],
|
|
['E', 'D', 'N', 'E+N'],
|
|
['E', 'R', 'N', 'E+R', 'E+N', 'ER'],
|
|
['E', 'D', 'R', 'E+R', 'ER'],
|
|
['E', 'D', 'N', 'E+N', 'EN'],
|
|
['E', 'R', 'E+R'],
|
|
['E'],
|
|
['N'],
|
|
];
|
|
|
|
private $sampleBasket = [
|
|
[1, 2, 3, 4],
|
|
[1, 2, 4],
|
|
[1, 2],
|
|
[2, 3, 4],
|
|
[2, 3],
|
|
[3, 4],
|
|
[2, 4],
|
|
];
|
|
|
|
public function testGreek(): void
|
|
{
|
|
$apriori = new Apriori(0.5, 0.5);
|
|
$apriori->train($this->sampleGreek, []);
|
|
|
|
$this->assertEquals('beta', $apriori->predict([['alpha', 'epsilon'], ['beta', 'theta']])[0][0][0]);
|
|
$this->assertEquals('alpha', $apriori->predict([['alpha', 'epsilon'], ['beta', 'theta']])[1][0][0]);
|
|
}
|
|
|
|
public function testPowerSet(): void
|
|
{
|
|
$apriori = new Apriori();
|
|
|
|
$this->assertCount(8, $this->invoke($apriori, 'powerSet', [['a', 'b', 'c']]));
|
|
}
|
|
|
|
public function testApriori(): void
|
|
{
|
|
$apriori = new Apriori(3 / 7);
|
|
$apriori->train($this->sampleBasket, []);
|
|
|
|
$L = $apriori->apriori();
|
|
|
|
$this->assertCount(0, $L[3]);
|
|
$this->assertCount(4, $L[2]);
|
|
$this->assertTrue($this->invoke($apriori, 'contains', [$L[2], [1, 2]]));
|
|
$this->assertFalse($this->invoke($apriori, 'contains', [$L[2], [1, 3]]));
|
|
$this->assertFalse($this->invoke($apriori, 'contains', [$L[2], [1, 4]]));
|
|
$this->assertTrue($this->invoke($apriori, 'contains', [$L[2], [2, 3]]));
|
|
$this->assertTrue($this->invoke($apriori, 'contains', [$L[2], [2, 4]]));
|
|
$this->assertTrue($this->invoke($apriori, 'contains', [$L[2], [3, 4]]));
|
|
}
|
|
|
|
public function testGetRules(): void
|
|
{
|
|
$apriori = new Apriori(0.4, 0.8);
|
|
$apriori->train($this->sampleChars, []);
|
|
|
|
$this->assertCount(19, $apriori->getRules());
|
|
}
|
|
|
|
public function testAntecedents(): void
|
|
{
|
|
$apriori = new Apriori();
|
|
|
|
$this->assertCount(6, $this->invoke($apriori, 'antecedents', [['a', 'b', 'c']]));
|
|
}
|
|
|
|
public function testItems(): void
|
|
{
|
|
$apriori = new Apriori();
|
|
$apriori->train($this->sampleGreek, []);
|
|
$this->assertCount(4, $this->invoke($apriori, 'items', []));
|
|
}
|
|
|
|
public function testFrequent(): void
|
|
{
|
|
$apriori = new Apriori(0.51);
|
|
$apriori->train($this->sampleGreek, []);
|
|
|
|
$this->assertCount(0, $this->invoke($apriori, 'frequent', [[['epsilon'], ['theta']]]));
|
|
$this->assertCount(2, $this->invoke($apriori, 'frequent', [[['alpha'], ['beta']]]));
|
|
}
|
|
|
|
public function testCandidates(): void
|
|
{
|
|
$apriori = new Apriori();
|
|
$apriori->train($this->sampleGreek, []);
|
|
|
|
$this->assertArraySubset([0 => ['alpha', 'beta']], $this->invoke($apriori, 'candidates', [[['alpha'], ['beta'], ['theta']]]));
|
|
$this->assertArraySubset([1 => ['alpha', 'theta']], $this->invoke($apriori, 'candidates', [[['alpha'], ['beta'], ['theta']]]));
|
|
$this->assertArraySubset([2 => ['beta', 'theta']], $this->invoke($apriori, 'candidates', [[['alpha'], ['beta'], ['theta']]]));
|
|
$this->assertCount(3, $this->invoke($apriori, 'candidates', [[['alpha'], ['beta'], ['theta']]]));
|
|
}
|
|
|
|
public function testConfidence(): void
|
|
{
|
|
$apriori = new Apriori();
|
|
$apriori->train($this->sampleGreek, []);
|
|
|
|
$this->assertEquals(0.5, $this->invoke($apriori, 'confidence', [['alpha', 'beta', 'theta'], ['alpha', 'beta']]));
|
|
$this->assertEquals(1, $this->invoke($apriori, 'confidence', [['alpha', 'beta'], ['alpha']]));
|
|
}
|
|
|
|
public function testSupport(): void
|
|
{
|
|
$apriori = new Apriori();
|
|
$apriori->train($this->sampleGreek, []);
|
|
|
|
$this->assertEquals(1.0, $this->invoke($apriori, 'support', [['alpha', 'beta']]));
|
|
$this->assertEquals(0.5, $this->invoke($apriori, 'support', [['epsilon']]));
|
|
}
|
|
|
|
public function testFrequency(): void
|
|
{
|
|
$apriori = new Apriori();
|
|
$apriori->train($this->sampleGreek, []);
|
|
|
|
$this->assertEquals(4, $this->invoke($apriori, 'frequency', [['alpha', 'beta']]));
|
|
$this->assertEquals(2, $this->invoke($apriori, 'frequency', [['epsilon']]));
|
|
}
|
|
|
|
public function testContains(): void
|
|
{
|
|
$apriori = new Apriori();
|
|
|
|
$this->assertTrue($this->invoke($apriori, 'contains', [[['a'], ['b']], ['a']]));
|
|
$this->assertTrue($this->invoke($apriori, 'contains', [[[1, 2]], [1, 2]]));
|
|
$this->assertFalse($this->invoke($apriori, 'contains', [[['a'], ['b']], ['c']]));
|
|
}
|
|
|
|
public function testSubset(): void
|
|
{
|
|
$apriori = new Apriori();
|
|
|
|
$this->assertTrue($this->invoke($apriori, 'subset', [['a', 'b'], ['a']]));
|
|
$this->assertTrue($this->invoke($apriori, 'subset', [['a'], ['a']]));
|
|
$this->assertFalse($this->invoke($apriori, 'subset', [['a'], ['a', 'b']]));
|
|
}
|
|
|
|
public function testEquals(): void
|
|
{
|
|
$apriori = new Apriori();
|
|
|
|
$this->assertTrue($this->invoke($apriori, 'equals', [['a'], ['a']]));
|
|
$this->assertFalse($this->invoke($apriori, 'equals', [['a'], []]));
|
|
$this->assertFalse($this->invoke($apriori, 'equals', [['a'], ['b', 'a']]));
|
|
}
|
|
|
|
/**
|
|
* Invokes objects method. Private/protected will be set accessible.
|
|
*
|
|
* @param string $method Method name to be called
|
|
* @param array $params Array of params to be passed
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function invoke(&$object, string $method, array $params = [])
|
|
{
|
|
$reflection = new ReflectionClass(get_class($object));
|
|
$method = $reflection->getMethod($method);
|
|
$method->setAccessible(true);
|
|
|
|
return $method->invokeArgs($object, $params);
|
|
}
|
|
|
|
public function testSaveAndRestore(): void
|
|
{
|
|
$classifier = new Apriori(0.5, 0.5);
|
|
$classifier->train($this->sampleGreek, []);
|
|
|
|
$testSamples = [['alpha', 'epsilon'], ['beta', 'theta']];
|
|
$predicted = $classifier->predict($testSamples);
|
|
|
|
$filename = 'apriori-test-'.random_int(100, 999).'-'.uniqid();
|
|
$filepath = tempnam(sys_get_temp_dir(), $filename);
|
|
$modelManager = new ModelManager();
|
|
$modelManager->saveToFile($classifier, $filepath);
|
|
|
|
$restoredClassifier = $modelManager->restoreFromFile($filepath);
|
|
$this->assertEquals($classifier, $restoredClassifier);
|
|
$this->assertEquals($predicted, $restoredClassifier->predict($testSamples));
|
|
}
|
|
}
|