1 | <?php |
||
10 | class Apriori implements Associator |
||
11 | { |
||
12 | use Trainable, Predictable; |
||
13 | |||
14 | public const ARRAY_KEY_ANTECEDENT = 'antecedent'; |
||
15 | |||
16 | public const ARRAY_KEY_CONFIDENCE = 'confidence'; |
||
17 | |||
18 | public const ARRAY_KEY_CONSEQUENT = 'consequent'; |
||
19 | |||
20 | public const ARRAY_KEY_SUPPORT = 'support'; |
||
21 | |||
22 | /** |
||
23 | * Minimum relative probability of frequent transactions. |
||
24 | * |
||
25 | * @var float |
||
26 | */ |
||
27 | private $confidence; |
||
28 | |||
29 | /** |
||
30 | * The large set contains frequent k-length item sets. |
||
31 | * |
||
32 | * @var mixed[][][] |
||
33 | */ |
||
34 | private $large = []; |
||
35 | |||
36 | /** |
||
37 | * Minimum relative frequency of transactions. |
||
38 | * |
||
39 | * @var float |
||
40 | */ |
||
41 | private $support; |
||
42 | |||
43 | /** |
||
44 | * The generated Apriori association rules. |
||
45 | * |
||
46 | * @var mixed[][] |
||
47 | */ |
||
48 | private $rules = []; |
||
49 | |||
50 | /** |
||
51 | * Apriori constructor. |
||
52 | */ |
||
53 | public function __construct(float $support = 0.0, float $confidence = 0.0) |
||
58 | |||
59 | /** |
||
60 | * Get all association rules which are generated for every k-length frequent item set. |
||
61 | * |
||
62 | * @return mixed[][] |
||
63 | */ |
||
64 | public function getRules(): array |
||
80 | |||
81 | /** |
||
82 | * Generates frequent item sets. |
||
83 | * |
||
84 | * @return mixed[][][] |
||
85 | */ |
||
86 | public function apriori(): array |
||
98 | |||
99 | /** |
||
100 | * @param mixed[] $sample |
||
101 | * |
||
102 | * @return mixed[][] |
||
103 | */ |
||
104 | protected function predictSample(array $sample): array |
||
114 | |||
115 | /** |
||
116 | * Generate rules for each k-length frequent item set. |
||
117 | */ |
||
118 | private function generateAllRules(): void |
||
126 | |||
127 | /** |
||
128 | * Generate confident rules for frequent item set. |
||
129 | * |
||
130 | * @param mixed[] $frequent |
||
131 | */ |
||
132 | private function generateRules(array $frequent): void |
||
147 | |||
148 | /** |
||
149 | * Generates the power set for given item set $sample. |
||
150 | * |
||
151 | * @param mixed[] $sample |
||
152 | * |
||
153 | * @return mixed[][] |
||
154 | */ |
||
155 | private function powerSet(array $sample): array |
||
166 | |||
167 | /** |
||
168 | * Generates all proper subsets for given set $sample without the empty set. |
||
169 | * |
||
170 | * @param mixed[] $sample |
||
171 | * |
||
172 | * @return mixed[][] |
||
173 | */ |
||
174 | private function antecedents(array $sample): array |
||
183 | |||
184 | /** |
||
185 | * Calculates frequent k = 1 item sets. |
||
186 | * |
||
187 | * @return mixed[][] |
||
188 | */ |
||
189 | private function items(): array |
||
205 | |||
206 | /** |
||
207 | * Returns frequent item sets only. |
||
208 | * |
||
209 | * @param mixed[][] $samples |
||
210 | * |
||
211 | * @return mixed[][] |
||
212 | */ |
||
213 | private function frequent(array $samples): array |
||
219 | |||
220 | /** |
||
221 | * Calculates frequent k item sets, where count($samples) == $k - 1. |
||
222 | * |
||
223 | * @param mixed[][] $samples |
||
224 | * |
||
225 | * @return mixed[][] |
||
226 | */ |
||
227 | private function candidates(array $samples): array |
||
255 | |||
256 | /** |
||
257 | * Calculates confidence for $set. Confidence is the relative amount of sets containing $subset which also contain |
||
258 | * $set. |
||
259 | * |
||
260 | * @param mixed[] $set |
||
261 | * @param mixed[] $subset |
||
262 | */ |
||
263 | private function confidence(array $set, array $subset): float |
||
267 | |||
268 | /** |
||
269 | * Calculates support for item set $sample. Support is the relative amount of sets containing $sample in the data |
||
270 | * pool. |
||
271 | * |
||
272 | * @see \Phpml\Association\Apriori::samples |
||
273 | * |
||
274 | * @param mixed[] $sample |
||
275 | */ |
||
276 | private function support(array $sample): float |
||
280 | |||
281 | /** |
||
282 | * Counts occurrences of $sample as subset in data pool. |
||
283 | * |
||
284 | * @see \Phpml\Association\Apriori::samples |
||
285 | * |
||
286 | * @param mixed[] $sample |
||
287 | */ |
||
288 | private function frequency(array $sample): int |
||
294 | |||
295 | /** |
||
296 | * Returns true if set is an element of system. |
||
297 | * |
||
298 | * @see \Phpml\Association\Apriori::equals() |
||
299 | * |
||
300 | * @param mixed[][] $system |
||
301 | * @param mixed[] $set |
||
302 | */ |
||
303 | private function contains(array $system, array $set): bool |
||
309 | |||
310 | /** |
||
311 | * Returns true if subset is a (proper) subset of set by its items string representation. |
||
312 | * |
||
313 | * @param mixed[] $set |
||
314 | * @param mixed[] $subset |
||
315 | */ |
||
316 | private function subset(array $set, array $subset): bool |
||
320 | |||
321 | /** |
||
322 | * Returns true if string representation of items does not differ. |
||
323 | * |
||
324 | * @param mixed[] $set1 |
||
325 | * @param mixed[] $set2 |
||
326 | */ |
||
327 | private function equals(array $set1, array $set2): bool |
||
331 | } |
||
332 |