Skip to content

Commit

Permalink
Fix apriori generates an empty array as a part of the frequent item s…
Browse files Browse the repository at this point in the history
…ets (#224)
  • Loading branch information
y-uti authored and akondas committed Feb 7, 2018
1 parent ec091b5 commit 71cc633
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/Phpml/Association/Apriori.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,11 @@ public function getRules(): array
public function apriori(): array
{
$L = [];
$L[1] = $this->items();
$L[1] = $this->frequent($L[1]);

for ($k = 2; !empty($L[$k - 1]); ++$k) {
$L[$k] = $this->candidates($L[$k - 1]);
$L[$k] = $this->frequent($L[$k]);
$items = $this->frequent($this->items());
for ($k = 1; !empty($items); ++$k) {
$L[$k] = $items;
$items = $this->frequent($this->candidates($items));
}

return $L;
Expand Down
26 changes: 25 additions & 1 deletion tests/Phpml/Association/AprioriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public function testApriori(): void

$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]]));
Expand Down Expand Up @@ -204,4 +203,29 @@ public function testSaveAndRestore(): void
$this->assertEquals($classifier, $restoredClassifier);
$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
{
$sample = [['a']];

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

$L = $apriori->apriori();

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

0 comments on commit 71cc633

Please sign in to comment.