Skip to content

Commit

Permalink
Fix support of a rule in Apriori (#229)
Browse files Browse the repository at this point in the history
* Clean up test code

* Add test to check support and confidence (failed due to a bug)

* Fix support value of rules
  • Loading branch information
y-uti authored and akondas committed Feb 11, 2018
1 parent 3ba3591 commit 53f8a89
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 71 deletions.
2 changes: 1 addition & 1 deletion src/Association/Apriori.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private function generateRules(array $frequent): void
$this->rules[] = [
self::ARRAY_KEY_ANTECEDENT => $antecedent,
self::ARRAY_KEY_CONSEQUENT => $consequent,
self::ARRAY_KEY_SUPPORT => $this->support($consequent),
self::ARRAY_KEY_SUPPORT => $this->support($frequent),
self::ARRAY_KEY_CONFIDENCE => $confidence,
];
}
Expand Down
169 changes: 99 additions & 70 deletions tests/Association/AprioriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,18 @@ 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]);
$predicted = $apriori->predict([['alpha', 'epsilon'], ['beta', 'theta']]);

$this->assertCount(2, $predicted);
$this->assertEquals([['beta']], $predicted[0]);
$this->assertEquals([['alpha']], $predicted[1]);
}

public function testPowerSet(): void
{
$apriori = new Apriori();

$this->assertCount(8, $this->invoke($apriori, 'powerSet', [['a', 'b', 'c']]));
$this->assertCount(8, self::invoke($apriori, 'powerSet', [['a', 'b', 'c']]));
}

public function testApriori(): void
Expand All @@ -65,12 +68,37 @@ public function testApriori(): void
$L = $apriori->apriori();

$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]]));
$this->assertTrue(self::invoke($apriori, 'contains', [$L[2], [1, 2]]));
$this->assertFalse(self::invoke($apriori, 'contains', [$L[2], [1, 3]]));
$this->assertFalse(self::invoke($apriori, 'contains', [$L[2], [1, 4]]));
$this->assertTrue(self::invoke($apriori, 'contains', [$L[2], [2, 3]]));
$this->assertTrue(self::invoke($apriori, 'contains', [$L[2], [2, 4]]));
$this->assertTrue(self::invoke($apriori, 'contains', [$L[2], [3, 4]]));
}

public function testAprioriEmpty(): void
{
$sample = [];

$apriori = new Apriori(0, 0);
$apriori->train($sample, []);

$L = $apriori->apriori();

$this->assertEmpty($L);
}

public function testAprioriSingleItem(): void
{
$sample = [['a']];

$apriori = new Apriori(0, 0);
$apriori->train($sample, []);

$L = $apriori->apriori();

$this->assertEquals([1], array_keys($L));
$this->assertEquals([['a']], $L[1]);
}

public function testGetRules(): void
Expand All @@ -81,109 +109,118 @@ public function testGetRules(): void
$this->assertCount(19, $apriori->getRules());
}

public function testGetRulesSupportAndConfidence(): void
{
$sample = [['a', 'b'], ['a', 'c']];

$apriori = new Apriori(0, 0);
$apriori->train($sample, []);

$rules = $apriori->getRules();

$this->assertCount(4, $rules);
$this->assertContains([
Apriori::ARRAY_KEY_ANTECEDENT => ['a'],
Apriori::ARRAY_KEY_CONSEQUENT => ['b'],
Apriori::ARRAY_KEY_SUPPORT => 0.5,
Apriori::ARRAY_KEY_CONFIDENCE => 0.5,
], $rules);
$this->assertContains([
Apriori::ARRAY_KEY_ANTECEDENT => ['b'],
Apriori::ARRAY_KEY_CONSEQUENT => ['a'],
Apriori::ARRAY_KEY_SUPPORT => 0.5,
Apriori::ARRAY_KEY_CONFIDENCE => 1.0,
], $rules);
}

public function testAntecedents(): void
{
$apriori = new Apriori();

$this->assertCount(6, $this->invoke($apriori, 'antecedents', [['a', 'b', 'c']]));
$this->assertCount(6, self::invoke($apriori, 'antecedents', [['a', 'b', 'c']]));
}

public function testItems(): void
{
$apriori = new Apriori();
$apriori->train($this->sampleGreek, []);
$this->assertCount(4, $this->invoke($apriori, 'items', []));
$this->assertCount(4, self::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']]]));
$this->assertCount(0, self::invoke($apriori, 'frequent', [[['epsilon'], ['theta']]]));
$this->assertCount(2, self::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']]]));
$candidates = self::invoke($apriori, 'candidates', [[['alpha'], ['beta'], ['theta']]]);

$this->assertCount(3, $candidates);
$this->assertEquals(['alpha', 'beta'], $candidates[0]);
$this->assertEquals(['alpha', 'theta'], $candidates[1]);
$this->assertEquals(['beta', 'theta'], $candidates[2]);
}

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']]));
$this->assertEquals(0.5, self::invoke($apriori, 'confidence', [['alpha', 'beta', 'theta'], ['alpha', 'beta']]));
$this->assertEquals(1, self::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']]));
$this->assertEquals(1.0, self::invoke($apriori, 'support', [['alpha', 'beta']]));
$this->assertEquals(0.5, self::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']]));
$this->assertEquals(4, self::invoke($apriori, 'frequency', [['alpha', 'beta']]));
$this->assertEquals(2, self::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']]));
$this->assertTrue(self::invoke($apriori, 'contains', [[['a'], ['b']], ['a']]));
$this->assertTrue(self::invoke($apriori, 'contains', [[[1, 2]], [1, 2]]));
$this->assertFalse(self::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']]));
$this->assertTrue(self::invoke($apriori, 'subset', [['a', 'b'], ['a']]));
$this->assertTrue(self::invoke($apriori, 'subset', [['a'], ['a']]));
$this->assertFalse(self::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);
$this->assertTrue(self::invoke($apriori, 'equals', [['a'], ['a']]));
$this->assertFalse(self::invoke($apriori, 'equals', [['a'], []]));
$this->assertFalse(self::invoke($apriori, 'equals', [['a'], ['b', 'a']]));
}

public function testSaveAndRestore(): void
Expand All @@ -204,28 +241,20 @@ public function testSaveAndRestore(): void
$this->assertEquals($predicted, $restoredClassifier->predict($testSamples));
}

public function testAprioriEmpty(): void
{
$sample = [];

$apriori = new Apriori(0, 0);
$apriori->train($sample, []);

$L = $apriori->apriori();

$this->assertEmpty($L);
}

public function testAprioriSingleItem(): void
/**
* 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
*/
private static function invoke(&$object, string $method, array $params = [])
{
$sample = [['a']];

$apriori = new Apriori(0, 0);
$apriori->train($sample, []);

$L = $apriori->apriori();
$reflection = new ReflectionClass(get_class($object));
$method = $reflection->getMethod($method);
$method->setAccessible(true);

$this->assertEquals([1], array_keys($L));
$this->assertEquals([['a']], $L[1]);
return $method->invokeArgs($object, $params);
}
}

0 comments on commit 53f8a89

Please sign in to comment.