|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace EnzoMC\PhpFPGrowth; |
| 5 | + |
| 6 | + |
| 7 | +class FPGrowth |
| 8 | +{ |
| 9 | + protected $support = 3; |
| 10 | + protected $confidence = 0.7; |
| 11 | + |
| 12 | + private $patterns; |
| 13 | + private $rules; |
| 14 | + |
| 15 | + /** |
| 16 | + * @return mixed |
| 17 | + */ |
| 18 | + public function getSupport() |
| 19 | + { |
| 20 | + return $this->support; |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * @param mixed $support |
| 25 | + */ |
| 26 | + public function setSupport($support) |
| 27 | + { |
| 28 | + $this->support = $support; |
| 29 | + return $this; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @return mixed |
| 34 | + */ |
| 35 | + public function getConfidence() |
| 36 | + { |
| 37 | + return $this->confidence; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @param mixed $confidence |
| 42 | + */ |
| 43 | + public function setConfidence($confidence) |
| 44 | + { |
| 45 | + $this->confidence = $confidence; |
| 46 | + return $this; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @return mixed |
| 51 | + */ |
| 52 | + public function getPatterns() |
| 53 | + { |
| 54 | + return $this->patterns; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @return mixed |
| 59 | + */ |
| 60 | + public function getRules() |
| 61 | + { |
| 62 | + return $this->rules; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * FPGrowth constructor. |
| 67 | + * @param $support 1, 2, 3 ... |
| 68 | + * @param $confidence 0 ... 1 |
| 69 | + */ |
| 70 | + public function __construct($support, $confidence) |
| 71 | + { |
| 72 | + $this->support = $support; |
| 73 | + $this->confidence = $confidence; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Do algorithm |
| 78 | + * @param $transactions |
| 79 | + */ |
| 80 | + public function run($transactions) |
| 81 | + { |
| 82 | + $this->patterns = $this->findFrequentPatterns($transactions, $this->support); |
| 83 | + $this->rules = $this->generateAssociationRules($this->patterns, $this->confidence); |
| 84 | + } |
| 85 | + |
| 86 | + protected function findFrequentPatterns($transactions, $support_threshold) |
| 87 | + { |
| 88 | + $tree = new FPTree($transactions, $support_threshold, null, null); |
| 89 | + return $tree->minePatterns($support_threshold); |
| 90 | + } |
| 91 | + |
| 92 | + protected function generateAssociationRules($patterns, $confidence_threshold) |
| 93 | + { |
| 94 | + $rules = []; |
| 95 | + foreach (array_keys($patterns) as $itemsetStr) { |
| 96 | + $itemset = explode(',', $itemsetStr); |
| 97 | + $upper_support = $patterns[$itemsetStr]; |
| 98 | + for ($i = 1; $i < count($itemset); $i++) { |
| 99 | + foreach (self::combinations($itemset, $i) as $antecedent) { |
| 100 | + sort($antecedent); |
| 101 | + $antecedentStr = implode(',', $antecedent); |
| 102 | + $consequent = array_diff($itemset, $antecedent); |
| 103 | + sort($consequent); |
| 104 | + $consequentStr = implode(',', $consequent); |
| 105 | + if (isset($patterns[$antecedentStr])) { |
| 106 | + $lower_support = $patterns[$antecedentStr]; |
| 107 | + $confidence = (floatval($upper_support) / $lower_support); |
| 108 | + if ($confidence >= $confidence_threshold) { |
| 109 | + $rules[$antecedentStr] = [$consequentStr, $confidence]; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + return $rules; |
| 116 | + } |
| 117 | + |
| 118 | + public static function iter($var) |
| 119 | + { |
| 120 | + |
| 121 | + switch (true) { |
| 122 | + case $var instanceof \Iterator: |
| 123 | + return $var; |
| 124 | + |
| 125 | + case $var instanceof \Traversable: |
| 126 | + return new \IteratorIterator($var); |
| 127 | + |
| 128 | + case is_string($var): |
| 129 | + $var = str_split($var); |
| 130 | + |
| 131 | + case is_array($var): |
| 132 | + return new \ArrayIterator($var); |
| 133 | + |
| 134 | + default: |
| 135 | + $type = gettype($var); |
| 136 | + throw new \InvalidArgumentException("'$type' type is not iterable"); |
| 137 | + } |
| 138 | + |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + public static function combinations($iterable, $r) |
| 143 | + { |
| 144 | + $pool = is_array($iterable) ? $iterable : iterator_to_array(self::iter($iterable)); |
| 145 | + $n = sizeof($pool); |
| 146 | + |
| 147 | + if ($r > $n) { |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + $indices = range(0, $r - 1); |
| 152 | + yield array_slice($pool, 0, $r); |
| 153 | + |
| 154 | + for (; ;) { |
| 155 | + for (; ;) { |
| 156 | + for ($i = $r - 1; $i >= 0; $i--) { |
| 157 | + if ($indices[$i] != $i + $n - $r) { |
| 158 | + break 2; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + $indices[$i]++; |
| 166 | + |
| 167 | + for ($j = $i + 1; $j < $r; $j++) { |
| 168 | + $indices[$j] = $indices[$j - 1] + 1; |
| 169 | + } |
| 170 | + |
| 171 | + $row = []; |
| 172 | + foreach ($indices as $i) { |
| 173 | + $row[] = $pool[$i]; |
| 174 | + } |
| 175 | + |
| 176 | + yield $row; |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments