mlxtend is a Python library of useful tools for machine learning that includes a lot of helper tools and algorithms that are not present in scikit-learn. One of its most interesting submodules is the one providing association rule inference.
Examples
Association Rule Learning in Python
This is a complete pipeline of association rule learning when using Python, delegating all the heavy-lifting to the mlxtend package.
Input data
The input format expects a set of transactions: sets of items that have been consumed together. For example, this list of sets is a valid starting point for this task:
Once these items are computed, we can call the assocation_rules function to perform the association rule learning and derive all the necessary metrics:
from mlxtend.frequent_patterns import association_rulesassociation_rules( frequent_itemsets, metric="confidence", min_threshold=0.7)# antecedents (Breaking, Cycling BMX Freestyle)# consequents (3x3 Basketball)# antecedent support 0.000306# consequent support 0.044237# support 0.00023# confidence 0.75# lift 16.954152# leverage 0.000216# conviction 3.823052# zhangs_metric 0.941306