2016-08-23 13:44:53 +00:00
# Apriori Associator
Association rule learning based on [Apriori algorithm ](https://en.wikipedia.org/wiki/Apriori_algorithm ) for frequent item set mining.
### Constructor Parameters
2018-02-27 17:50:07 +00:00
* $support - minimum threshold of [support ](https://en.wikipedia.org/wiki/Association_rule_learning#Support ), i.e. the ratio of samples which contain both X and Y for a rule "if X then Y"
* $confidence - minimum threshold of [confidence ](https://en.wikipedia.org/wiki/Association_rule_learning#Confidence ), i.e. the ratio of samples containing both X and Y to those containing X
2016-08-23 13:44:53 +00:00
```
2016-09-01 22:18:50 +00:00
use Phpml\Association\Apriori;
$associator = new Apriori($support = 0.5, $confidence = 0.5);
2016-08-23 13:44:53 +00:00
```
### Train
To train a associator simply provide train samples and labels (as `array` ). Example:
```
$samples = [['alpha', 'beta', 'epsilon'], ['alpha', 'beta', 'theta'], ['alpha', 'beta', 'epsilon'], ['alpha', 'beta', 'theta']];
$labels = [];
2016-09-01 22:18:50 +00:00
use Phpml\Association\Apriori;
$associator = new Apriori($support = 0.5, $confidence = 0.5);
2016-08-23 13:44:53 +00:00
$associator->train($samples, $labels);
```
2017-02-01 18:06:38 +00:00
You can train the associator using multiple data sets, predictions will be based on all the training data.
2016-08-23 13:44:53 +00:00
### Predict
To predict sample label use `predict` method. You can provide one sample or array of samples:
```
$associator->predict(['alpha','theta']);
2018-02-05 17:50:45 +00:00
// return [['beta']]
2016-08-23 13:44:53 +00:00
$associator->predict([['alpha','epsilon'],['beta','theta']]);
// return [[['beta']], [['alpha']]]
```
### Associating
2016-09-01 22:18:50 +00:00
Get generated association rules simply use `rules` method.
2018-02-27 17:50:07 +00:00
2016-08-23 13:44:53 +00:00
```
2016-09-01 22:18:50 +00:00
$associator->getRules();
2018-02-05 17:50:45 +00:00
// return [['antecedent' => ['alpha', 'theta'], 'consequent' => ['beta'], 'support' => 1.0, 'confidence' => 1.0], ... ]
2016-08-23 13:44:53 +00:00
```
### Frequent item sets
Generating k-length frequent item sets simply use `apriori` method.
```
$associator->apriori();
// return [ 1 => [['alpha'], ['beta'], ['theta'], ['epsilon']], 2 => [...], ...]
```