2016-08-23 13:44:53 +00:00
|
|
|
<?php
|
|
|
|
|
2016-11-20 21:53:17 +00:00
|
|
|
declare(strict_types=1);
|
2016-08-23 13:44:53 +00:00
|
|
|
|
2018-01-06 12:09:33 +00:00
|
|
|
namespace Phpml\Tests\Association;
|
2016-08-23 13:44:53 +00:00
|
|
|
|
|
|
|
use Phpml\Association\Apriori;
|
2017-02-02 08:03:09 +00:00
|
|
|
use Phpml\ModelManager;
|
2017-02-03 11:58:25 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-11-22 21:16:10 +00:00
|
|
|
use ReflectionClass;
|
2016-08-23 13:44:53 +00:00
|
|
|
|
2017-02-03 11:58:25 +00:00
|
|
|
class AprioriTest extends TestCase
|
2016-08-23 13:44:53 +00:00
|
|
|
{
|
2018-10-28 06:44:52 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-09-21 19:51:19 +00:00
|
|
|
private $sampleGreek = [
|
2016-08-23 13:44:53 +00:00
|
|
|
['alpha', 'beta', 'epsilon'],
|
|
|
|
['alpha', 'beta', 'theta'],
|
|
|
|
['alpha', 'beta', 'epsilon'],
|
|
|
|
['alpha', 'beta', 'theta'],
|
|
|
|
];
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-09-21 19:51:19 +00:00
|
|
|
private $sampleChars = [
|
2016-08-23 13:44:53 +00:00
|
|
|
['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'],
|
2016-09-21 19:51:19 +00:00
|
|
|
['N'],
|
2016-08-23 13:44:53 +00:00
|
|
|
];
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-08-23 13:44:53 +00:00
|
|
|
private $sampleBasket = [
|
|
|
|
[1, 2, 3, 4],
|
|
|
|
[1, 2, 4],
|
|
|
|
[1, 2],
|
|
|
|
[2, 3, 4],
|
|
|
|
[2, 3],
|
|
|
|
[3, 4],
|
|
|
|
[2, 4],
|
|
|
|
];
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testGreek(): void
|
2016-08-23 13:44:53 +00:00
|
|
|
{
|
|
|
|
$apriori = new Apriori(0.5, 0.5);
|
|
|
|
$apriori->train($this->sampleGreek, []);
|
|
|
|
|
2018-02-11 11:42:46 +00:00
|
|
|
$predicted = $apriori->predict([['alpha', 'epsilon'], ['beta', 'theta']]);
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertCount(2, $predicted);
|
|
|
|
self::assertEquals([['beta']], $predicted[0]);
|
|
|
|
self::assertEquals([['alpha']], $predicted[1]);
|
2016-08-23 13:44:53 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testPowerSet(): void
|
2016-08-23 13:44:53 +00:00
|
|
|
{
|
|
|
|
$apriori = new Apriori();
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertCount(8, self::invoke($apriori, 'powerSet', [['a', 'b', 'c']]));
|
2016-08-23 13:44:53 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testApriori(): void
|
2016-08-23 13:44:53 +00:00
|
|
|
{
|
|
|
|
$apriori = new Apriori(3 / 7);
|
|
|
|
$apriori->train($this->sampleBasket, []);
|
|
|
|
|
|
|
|
$L = $apriori->apriori();
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertCount(4, $L[2]);
|
|
|
|
self::assertTrue(self::invoke($apriori, 'contains', [$L[2], [1, 2]]));
|
|
|
|
self::assertFalse(self::invoke($apriori, 'contains', [$L[2], [1, 3]]));
|
|
|
|
self::assertFalse(self::invoke($apriori, 'contains', [$L[2], [1, 4]]));
|
|
|
|
self::assertTrue(self::invoke($apriori, 'contains', [$L[2], [2, 3]]));
|
|
|
|
self::assertTrue(self::invoke($apriori, 'contains', [$L[2], [2, 4]]));
|
|
|
|
self::assertTrue(self::invoke($apriori, 'contains', [$L[2], [3, 4]]));
|
2018-02-11 11:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAprioriEmpty(): void
|
|
|
|
{
|
|
|
|
$sample = [];
|
|
|
|
|
|
|
|
$apriori = new Apriori(0, 0);
|
|
|
|
$apriori->train($sample, []);
|
|
|
|
|
|
|
|
$L = $apriori->apriori();
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEmpty($L);
|
2018-02-11 11:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAprioriSingleItem(): void
|
|
|
|
{
|
|
|
|
$sample = [['a']];
|
|
|
|
|
|
|
|
$apriori = new Apriori(0, 0);
|
|
|
|
$apriori->train($sample, []);
|
|
|
|
|
|
|
|
$L = $apriori->apriori();
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals([1], array_keys($L));
|
|
|
|
self::assertEquals([['a']], $L[1]);
|
2016-08-23 13:44:53 +00:00
|
|
|
}
|
|
|
|
|
2018-02-22 16:02:55 +00:00
|
|
|
public function testAprioriL3(): void
|
|
|
|
{
|
|
|
|
$sample = [['a', 'b', 'c']];
|
|
|
|
|
|
|
|
$apriori = new Apriori(0, 0);
|
|
|
|
$apriori->train($sample, []);
|
|
|
|
|
|
|
|
$L = $apriori->apriori();
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals([['a', 'b', 'c']], $L[3]);
|
2018-02-22 16:02:55 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testGetRules(): void
|
2016-08-23 13:44:53 +00:00
|
|
|
{
|
|
|
|
$apriori = new Apriori(0.4, 0.8);
|
|
|
|
$apriori->train($this->sampleChars, []);
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertCount(19, $apriori->getRules());
|
2016-08-23 13:44:53 +00:00
|
|
|
}
|
|
|
|
|
2018-02-11 11:42:46 +00:00
|
|
|
public function testGetRulesSupportAndConfidence(): void
|
|
|
|
{
|
|
|
|
$sample = [['a', 'b'], ['a', 'c']];
|
|
|
|
|
|
|
|
$apriori = new Apriori(0, 0);
|
|
|
|
$apriori->train($sample, []);
|
|
|
|
|
|
|
|
$rules = $apriori->getRules();
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertCount(4, $rules);
|
|
|
|
self::assertContains([
|
2018-02-11 11:42:46 +00:00
|
|
|
Apriori::ARRAY_KEY_ANTECEDENT => ['a'],
|
|
|
|
Apriori::ARRAY_KEY_CONSEQUENT => ['b'],
|
|
|
|
Apriori::ARRAY_KEY_SUPPORT => 0.5,
|
|
|
|
Apriori::ARRAY_KEY_CONFIDENCE => 0.5,
|
|
|
|
], $rules);
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertContains([
|
2018-02-11 11:42:46 +00:00
|
|
|
Apriori::ARRAY_KEY_ANTECEDENT => ['b'],
|
|
|
|
Apriori::ARRAY_KEY_CONSEQUENT => ['a'],
|
|
|
|
Apriori::ARRAY_KEY_SUPPORT => 0.5,
|
|
|
|
Apriori::ARRAY_KEY_CONFIDENCE => 1.0,
|
|
|
|
], $rules);
|
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testAntecedents(): void
|
2016-08-23 13:44:53 +00:00
|
|
|
{
|
|
|
|
$apriori = new Apriori();
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertCount(6, self::invoke($apriori, 'antecedents', [['a', 'b', 'c']]));
|
2016-08-23 13:44:53 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testItems(): void
|
2016-08-23 13:44:53 +00:00
|
|
|
{
|
|
|
|
$apriori = new Apriori();
|
|
|
|
$apriori->train($this->sampleGreek, []);
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertCount(4, self::invoke($apriori, 'items', []));
|
2016-08-23 13:44:53 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testFrequent(): void
|
2016-08-23 13:44:53 +00:00
|
|
|
{
|
|
|
|
$apriori = new Apriori(0.51);
|
|
|
|
$apriori->train($this->sampleGreek, []);
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertCount(0, self::invoke($apriori, 'frequent', [[['epsilon'], ['theta']]]));
|
|
|
|
self::assertCount(2, self::invoke($apriori, 'frequent', [[['alpha'], ['beta']]]));
|
2016-08-23 13:44:53 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testCandidates(): void
|
2016-08-23 13:44:53 +00:00
|
|
|
{
|
|
|
|
$apriori = new Apriori();
|
|
|
|
$apriori->train($this->sampleGreek, []);
|
|
|
|
|
2018-02-11 11:42:46 +00:00
|
|
|
$candidates = self::invoke($apriori, 'candidates', [[['alpha'], ['beta'], ['theta']]]);
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertCount(3, $candidates);
|
|
|
|
self::assertEquals(['alpha', 'beta'], $candidates[0]);
|
|
|
|
self::assertEquals(['alpha', 'theta'], $candidates[1]);
|
|
|
|
self::assertEquals(['beta', 'theta'], $candidates[2]);
|
2016-08-23 13:44:53 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testConfidence(): void
|
2016-08-23 13:44:53 +00:00
|
|
|
{
|
|
|
|
$apriori = new Apriori();
|
|
|
|
$apriori->train($this->sampleGreek, []);
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals(0.5, self::invoke($apriori, 'confidence', [['alpha', 'beta', 'theta'], ['alpha', 'beta']]));
|
|
|
|
self::assertEquals(1, self::invoke($apriori, 'confidence', [['alpha', 'beta'], ['alpha']]));
|
2016-08-23 13:44:53 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testSupport(): void
|
2016-08-23 13:44:53 +00:00
|
|
|
{
|
|
|
|
$apriori = new Apriori();
|
|
|
|
$apriori->train($this->sampleGreek, []);
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals(1.0, self::invoke($apriori, 'support', [['alpha', 'beta']]));
|
|
|
|
self::assertEquals(0.5, self::invoke($apriori, 'support', [['epsilon']]));
|
2016-08-23 13:44:53 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testFrequency(): void
|
2016-08-23 13:44:53 +00:00
|
|
|
{
|
|
|
|
$apriori = new Apriori();
|
|
|
|
$apriori->train($this->sampleGreek, []);
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals(4, self::invoke($apriori, 'frequency', [['alpha', 'beta']]));
|
|
|
|
self::assertEquals(2, self::invoke($apriori, 'frequency', [['epsilon']]));
|
2016-08-23 13:44:53 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testContains(): void
|
2016-08-23 13:44:53 +00:00
|
|
|
{
|
|
|
|
$apriori = new Apriori();
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertTrue(self::invoke($apriori, 'contains', [[['a'], ['b']], ['a']]));
|
|
|
|
self::assertTrue(self::invoke($apriori, 'contains', [[[1, 2]], [1, 2]]));
|
|
|
|
self::assertFalse(self::invoke($apriori, 'contains', [[['a'], ['b']], ['c']]));
|
2016-08-23 13:44:53 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testSubset(): void
|
2016-08-23 13:44:53 +00:00
|
|
|
{
|
|
|
|
$apriori = new Apriori();
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertTrue(self::invoke($apriori, 'subset', [['a', 'b'], ['a']]));
|
|
|
|
self::assertTrue(self::invoke($apriori, 'subset', [['a'], ['a']]));
|
|
|
|
self::assertFalse(self::invoke($apriori, 'subset', [['a'], ['a', 'b']]));
|
2016-08-23 13:44:53 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testEquals(): void
|
2016-08-23 13:44:53 +00:00
|
|
|
{
|
|
|
|
$apriori = new Apriori();
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertTrue(self::invoke($apriori, 'equals', [['a'], ['a']]));
|
|
|
|
self::assertFalse(self::invoke($apriori, 'equals', [['a'], []]));
|
|
|
|
self::assertFalse(self::invoke($apriori, 'equals', [['a'], ['b', 'a']]));
|
2016-08-23 13:44:53 +00:00
|
|
|
}
|
2017-02-02 08:03:09 +00:00
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testSaveAndRestore(): void
|
2017-02-02 08:03:09 +00:00
|
|
|
{
|
|
|
|
$classifier = new Apriori(0.5, 0.5);
|
|
|
|
$classifier->train($this->sampleGreek, []);
|
|
|
|
|
|
|
|
$testSamples = [['alpha', 'epsilon'], ['beta', 'theta']];
|
|
|
|
$predicted = $classifier->predict($testSamples);
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
$filename = 'apriori-test-'.random_int(100, 999).'-'.uniqid('', false);
|
|
|
|
$filepath = (string) tempnam(sys_get_temp_dir(), $filename);
|
2017-02-02 08:03:09 +00:00
|
|
|
$modelManager = new ModelManager();
|
|
|
|
$modelManager->saveToFile($classifier, $filepath);
|
|
|
|
|
|
|
|
$restoredClassifier = $modelManager->restoreFromFile($filepath);
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals($classifier, $restoredClassifier);
|
|
|
|
self::assertEquals($predicted, $restoredClassifier->predict($testSamples));
|
2017-02-02 08:03:09 +00:00
|
|
|
}
|
2018-02-07 09:02:38 +00:00
|
|
|
|
2018-02-11 11:42:46 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2018-10-28 06:44:52 +00:00
|
|
|
private static function invoke(Apriori $object, string $method, array $params = [])
|
2018-02-07 09:02:38 +00:00
|
|
|
{
|
2018-02-11 11:42:46 +00:00
|
|
|
$reflection = new ReflectionClass(get_class($object));
|
|
|
|
$method = $reflection->getMethod($method);
|
|
|
|
$method->setAccessible(true);
|
2018-02-07 09:02:38 +00:00
|
|
|
|
2018-02-11 11:42:46 +00:00
|
|
|
return $method->invokeArgs($object, $params);
|
2018-02-07 09:02:38 +00:00
|
|
|
}
|
2016-08-23 13:44:53 +00:00
|
|
|
}
|