association_rules: Generating Association Rules from Frequent Itemsets (mlxtend)#

Stage 3 · 🛒 Market Basket & Association Rules · Lesson 21 of 56 · intermediate

◀ Previous · Apriori: Frequent Itemsets via the Apriori Algorithm · Next · Cross-Selling ▶ · ↑ Section

Important

✨ AI-generated content. This page was written with the assistance of an AI language model and is provided as a learning aid. Despite careful review, it may still contain mistakes, omissions, or out-of-date information. Whether you are new to the topic, a team lead, or a senior practitioner, treat it as a starting point rather than an authoritative reference: read it critically and independently verify anything you act on (code, commands, figures, and factual claims) against official documentation and primary sources before relying on it.

From itemsets to rules in code#

The Python library mlxtend implements the whole pipeline of the last two lessons in a few lines: encode the transactions, mine frequent itemsets with apriori, then turn them into ranked rules with association_rules. It is the standard tool for market-basket analysis in the scientific-Python stack.

One-hot transactions#

The algorithms expect a one-hot encoded table — one row per transaction, one boolean column per item, True where the item is in the basket. mlxtend’s TransactionEncoder builds it from raw lists of items:

from mlxtend.preprocessing import TransactionEncoder
import pandas as pd

transactions = [["bread", "milk", "eggs"], ["bread", "butter"], ["milk", "butter"]]
te = TransactionEncoder()
df = pd.DataFrame(te.fit_transform(transactions), columns=te.columns_)

Two functions#

With the table ready, two calls do the work:

from mlxtend.frequent_patterns import apriori, association_rules

items = apriori(df, min_support=0.5, use_colnames=True)
rules = association_rules(items, metric="confidence", min_threshold=0.6)

apriori returns the frequent itemsets and their support; association_rules expands them into rules and filters by the metric you choose ("confidence", "lift", and others).

Reading the output#

The result is a tidy DataFrame: each row a rule, with columns for antecedents, consequents, support, confidence and lift (plus leverage and conviction). Sorting by lift surfaces the most surprising, actionable pairings — the rules a shop would actually act on. The final lesson of this stage puts them to use: cross-selling.

See also

Source article Adapted (context, re-expressed) in our own words from: https://insightful-data-lab.com/2026/01/14/association_rules-generating-association-rules-from-frequent-itemsets-mlxtend/ (insightful-data-lab.com).

Tags: purpose: reference topic: data analysis topic: data preparation level: intermediate