Fix apriori generates an empty array as a part of the frequent item sets (#224)

This commit is contained in:
Yuji Uchiyama 2018-02-07 18:02:38 +09:00 committed by Arkadiusz Kondas
parent ec091b5ea3
commit 71cc633c8e
2 changed files with 29 additions and 6 deletions

View File

@ -86,12 +86,11 @@ class Apriori implements Associator
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;

View File

@ -64,7 +64,6 @@ class AprioriTest extends TestCase
$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]]));
@ -204,4 +203,29 @@ class AprioriTest extends TestCase
$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]);
}
}