Fix apriori keys (#238)

* Add test to check keys of rules

* Reindex after array_filter/array_unique in Apriori
This commit is contained in:
Yuji Uchiyama 2018-02-23 01:02:55 +09:00 committed by Arkadiusz Kondas
parent 8aed3b9286
commit add00c6108
2 changed files with 15 additions and 3 deletions

View File

@ -212,9 +212,9 @@ class Apriori implements Associator
*/ */
private function frequent(array $samples): array private function frequent(array $samples): array
{ {
return array_filter($samples, function ($entry) { return array_values(array_filter($samples, function ($entry) {
return $this->support($entry) >= $this->support; return $this->support($entry) >= $this->support;
}); }));
} }
/** /**
@ -234,7 +234,7 @@ class Apriori implements Associator
continue; continue;
} }
$candidate = array_unique(array_merge($p, $q)); $candidate = array_values(array_unique(array_merge($p, $q)));
if ($this->contains($candidates, $candidate)) { if ($this->contains($candidates, $candidate)) {
continue; continue;

View File

@ -101,6 +101,18 @@ class AprioriTest extends TestCase
$this->assertEquals([['a']], $L[1]); $this->assertEquals([['a']], $L[1]);
} }
public function testAprioriL3(): void
{
$sample = [['a', 'b', 'c']];
$apriori = new Apriori(0, 0);
$apriori->train($sample, []);
$L = $apriori->apriori();
$this->assertEquals([['a', 'b', 'c']], $L[3]);
}
public function testGetRules(): void public function testGetRules(): void
{ {
$apriori = new Apriori(0.4, 0.8); $apriori = new Apriori(0.4, 0.8);