mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2025-01-24 15:48:24 +00:00
Fix apriori generates an empty array as a part of the frequent item sets (#224)
This commit is contained in:
parent
ec091b5ea3
commit
71cc633c8e
@ -86,12 +86,11 @@ class Apriori implements Associator
|
|||||||
public function apriori(): array
|
public function apriori(): array
|
||||||
{
|
{
|
||||||
$L = [];
|
$L = [];
|
||||||
$L[1] = $this->items();
|
|
||||||
$L[1] = $this->frequent($L[1]);
|
|
||||||
|
|
||||||
for ($k = 2; !empty($L[$k - 1]); ++$k) {
|
$items = $this->frequent($this->items());
|
||||||
$L[$k] = $this->candidates($L[$k - 1]);
|
for ($k = 1; !empty($items); ++$k) {
|
||||||
$L[$k] = $this->frequent($L[$k]);
|
$L[$k] = $items;
|
||||||
|
$items = $this->frequent($this->candidates($items));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $L;
|
return $L;
|
||||||
|
@ -64,7 +64,6 @@ class AprioriTest extends TestCase
|
|||||||
|
|
||||||
$L = $apriori->apriori();
|
$L = $apriori->apriori();
|
||||||
|
|
||||||
$this->assertCount(0, $L[3]);
|
|
||||||
$this->assertCount(4, $L[2]);
|
$this->assertCount(4, $L[2]);
|
||||||
$this->assertTrue($this->invoke($apriori, 'contains', [$L[2], [1, 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, 3]]));
|
||||||
@ -204,4 +203,29 @@ class AprioriTest extends TestCase
|
|||||||
$this->assertEquals($classifier, $restoredClassifier);
|
$this->assertEquals($classifier, $restoredClassifier);
|
||||||
$this->assertEquals($predicted, $restoredClassifier->predict($testSamples));
|
$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]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user