Help language development. Donate to The Perl Foundation

ML::AssociationRuleLearning zef:antononcube last updated on 2022-08-20
# Raku ML::AssociationRuleLearning

[![License: Artistic-2.0](](

This repository has the code of a Raku package for
[Association Rule Learning (ARL)](
functions, [Wk1].

The ARL framework includes the algorithms 
and the measures 
[lift](, and 
(and others.)

For computational introduction to ARL utilization (in Mathematica) see the article
["Movie genre associations"](,

The examples below use the packages
["Data::Reshapers"](, and
["Data::Summarizers"](, described in the article
["Introduction to data wrangling with Raku"](,


## Installation

Via zef-ecosystem:

zef install ML::AssociationRuleLearning

From GitHub:

zef install


## Frequent sets finding 

Here we get the Titanic dataset (from "Data::Reshapers") and summarize it:

use Data::Reshapers;
use Data::Summarizers;
my @dsTitanic = get-titanic-dataset();

**Problem:** Find all combinations of values of the variables "passengerAge", "passengerClass", "passengerSex", and
"passengerSurvival" that appear more than 200 times in the Titanic dataset.

Here is how we use the function `frequent-sets` to give an answer:

use ML::AssociationRuleLearning;
my @freqSets = frequent-sets(@dsTitanic, min-support => 200, min-number-of-items => 2, max-number-of-items => Inf):counts;

The function `frequent-sets` returns the frequent sets together with their support.

Here we tabulate the result:

say to-pretty-table({ %( Frequent-set => $_.key.join(' '), Count => $_.value) }), align => 'l');

We can verify the result by looking into these group counts, [AA2]:

my $obj = group-by( @dsTitanic, <passengerClass passengerSex>);
.say for $obj>>.elems.grep({ $_.value >= 200 });
$obj = group-by( @dsTitanic, <passengerClass passengerSurvival passengerSex>);
.say for $obj>>.elems.grep({ $_.value >= 200 });

Or these contingency tables:

my $obj = group-by( @dsTitanic, "passengerClass") ;
$obj = ${ $_.key => cross-tabulate( $_.value, "passengerSex", "passengerSurvival" ) });
.say for $obj.Array;

**Remark:** For datasets -- i.e. arrays of hashes -- `frequent-sets` preprocesses the data by concatenating
column names with corresponding column values. This is done in order to prevent "collisions" of same values 
coming from different columns. If that concatenation is not desired then manual preprocessing like this can be used:

```{perl6, eval=FALSE}{ $_.values.List }).Array

**Remark:** `frequent-sets`'s argument `min-support` can take both integers greater than 1 and frequencies between 0 and 1.
(If an integer greater than one is given, then the corresponding frequency is derived.)

**Remark:** By default `frequent-sets` uses the Eclat algorithm. The functions `apriori` and `eclat`
call `frequent-sets` with the option settings `method=>'Apriori'` and `method=>'Eclat'` respectively.


## Association rules finding

Here we find association rules with min support 0.3 and min confidence 0.7:

association-rules(@dsTitanic, min-support => 0.3, min-confidence => 0.7)
==> to-pretty-table

### Reusing found frequent sets

The function `frequent-sets` takes the adverb ":object" that makes `frequent-sets` return an object of type
`ML::AssociationRuleLearning::Apriori` or `ML::AssociationRuleLearning::Eclat`, 
which can be "pipelined" to find association rules.

Here we find frequent sets, return the corresponding object, and retrieve the result:

my $eclatObj = frequent-sets({ $_.values.List }).Array, min-support => 0.12, min-number-of-items => 2, max-number-of-items => 6):object;

Here we find association rules and pretty-print them:

==> to-pretty-table 

**Remark:** Note that because of the specified min confidence, the number of association rules is "contained" --
a (much) larger number of rules would be produced with, say, `min-confidence=>0.2`.


## Implementation considerations

### UML diagram

Here is a UML diagram that shows package's structure:


[PlantUML spec](./resources/class-diagram.puml)
were obtained with the CLI script `to-uml-spec` of the package "UML::Translators", [AAp6].

Here we get the [PlantUML spec](./resources/class-diagram.puml):

to-uml-spec ML::AssociationRuleLearning > ./resources/class-diagram.puml

Here get the [diagram](./resources/class-diagram.png):

to-uml-spec ML::AssociationRuleLearning | java -jar ~/PlantUML/plantuml-1.2022.5.jar -pipe > ./resources/class-diagram.png

**Remark:** Maybe it is a good idea to have an abstract class named, say,
`ML::AssociationRuleLearning::AbstractFinder` that is a parent of both
`ML::AssociationRuleLearning::Apriori` and `ML::AssociationRuleLearning::Eclat`,
but I have not found to be necessary. (At this point of development.)

### Eclat

We can say that Eclat uses a "vertical database representation" of the transactions.

Eclat is based on Raku's 
[sets, bags, and mixes](

Eclat represents the transactions as a hash of sets:

- The keys of the hash are items

- The elements of the sets are transaction identifiers.

(In other words, for each item an inverse index is made.)

This representation allows for quick calculations of item combinations support.

### Apriori 

Apriori uses the standard, horizontal database transactions representation.

We can say that Apriori:

- Generates candidates for item frequent sets using the routine 

- Filters candidates by 
  [Tries with frequencies]( 
  creation and removal by threshold

Apriori is usually (much) slower than Eclat. 
Historically, Apriori is the first ARL method, and its implementation in the package is didactic.

### Association rules

We can say that the association rule finding function is a general one, but that function
does require fast computation of confidence, lift, etc. Hence Eclat's transactions representation
is used.

Association rules finding with Apriori is the same as with Eclat. 
The package function `assocition-rules` with the option setting `method=>'Apriori'`
simply sends frequent sets found with Apriori to the Eclat based association rule finding.


## References

### Articles

[Wk1] Wikipedia entry, ["Association Rule Learning"](

[AA1] Anton Antonov,
["Movie genre associations"](,
[MathematicaForPrediction at WordPress](

[AA2] Anton Antonov,
["Introduction to data wrangling with Raku"](,
[RakuForPrediction at WordPress](

### Packages

[AAp1] Anton Antonov,
[Implementation of the Apriori algorithm in Mathematica](,
[MathematicaForPrediction at GitHub/antononcube](

[AAp1a] Anton Antonov
[Implementation of the Apriori algorithm via Tries in Mathematica](,
[MathematicaForPrediction at GitHub/antononcube](

[AAp2] Anton Antonov,
[Implementation of the Eclat algorithm in Mathematica](,
[MathematicaForPrediction at GitHub/antononcube](

[AAp3] Anton Antonov,
[Data::Generators Raku package](,

[AAp4] Anton Antonov,
[Data::Reshapers Raku package](,

[AAp5] Anton Antonov,
[Data::Summarizers Raku package](,

[AAp6] Anton Antonov,
[UML::Translators Raku package](,

[AAp7] Anton Antonov,
[ML::TrieWithFrequencies Raku package](,