
Image by Author
You’ve generated 500 association rules from your transaction data, but most are useless. Rules like “customers who buy water also buy bread” have high support simply because both items are popular, not because they’re meaningfully related. Filtering rules using multiple metrics at once separates genuine patterns from statistical noise.
This guide shows you how to apply support, confidence, and lift thresholds together to find rules worth acting on.
Why Single Metrics Fail
Using only one threshold creates problems. A high support threshold keeps popular items but misses valuable niche combinations. A confidence threshold alone can flag rules where items appear together purely by chance. Lift helps identify genuine associations, but extremely high lift values might represent rare, unreliable patterns.
The solution is filtering with all three metrics together. Support confirms patterns appear often enough to matter, confidence shows predictive strength, and lift verifies the relationship exceeds random chance. This multi-metric approach balances popularity, prediction accuracy, and relationship strength.
Setting Up the Bookstore Data
We’ll use a synthetic bookstore dataset with 25 transactions across 15 book titles.
import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori, association_rules
transactions = [
['Harry Potter', 'Hunger Games'],
['Hunger Games', 'Divergent'],
['Harry Potter', 'Twilight'],
['Divergent', 'Hunger Games', 'Twilight'],
['Harry Potter', 'Hunger Games', 'Percy Jackson'],
['Lord of the Rings', 'Hobbit'],
['1984', 'Brave New World'],
['Harry Potter', 'Lord of the Rings'],
['Hunger Games', 'Twilight'],
['Divergent', 'Maze Runner'],
['Harry Potter', 'Hunger Games'],
['Twilight', 'Vampire Academy'],
['Lord of the Rings', 'Hobbit', 'Game of Thrones'],
['Harry Potter', 'Divergent'],
['Hunger Games', 'Divergent'],
['1984', 'Animal Farm'],
['Harry Potter', 'Hunger Games', 'Twilight'],
['Lord of the Rings', 'Silmarillion'],
['Divergent', 'Hunger Games'],
['Harry Potter', 'Percy Jackson'],
['Twilight', 'Hunger Games'],
['Lord of the Rings', 'Hobbit'],
['Harry Potter', 'Hunger Games'],
['Divergent', 'Insurgent'],
['Harry Potter', 'Twilight', 'Hunger Games']
]
encoder = TransactionEncoder()
onehot_array = encoder.fit_transform(transactions)
onehot_df = pd.DataFrame(onehot_array, columns=encoder.columns_)
This creates the one-hot encoded format needed for analysis.
Generating Association Rules
First, find frequent itemsets, then generate all possible rules. Understanding metrics like support, confidence, and lift helps interpret these results.
frequent_itemsets = apriori(onehot_df, min_support=0.08, use_colnames=True)
rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.3)
print(f"Generated {len(rules)} rules before filtering")
print("\nAll rules with their metrics:")
print(rules[['antecedents', 'consequents', 'support',
'confidence', 'lift']].to_string())
This generates 14 rules meeting basic thresholds.
Generated 14 rules before filtering
All rules with their metrics:
antecedents consequents support confidence lift
0 (Hunger Games) (Divergent) 0.16 0.333333 1.190476
1 (Divergent) (Hunger Games) 0.16 0.571429 1.190476
2 (Hunger Games) (Harry Potter) 0.24 0.500000 1.250000
3 (Harry Potter) (Hunger Games) 0.24 0.600000 1.250000
4 (Percy Jackson) (Harry Potter) 0.08 1.000000 2.500000
5 (Harry Potter) (Twilight) 0.12 0.300000 1.071429
6 (Twilight) (Harry Potter) 0.12 0.428571 1.071429
7 (Lord of the Rings) (Hobbit) 0.12 0.600000 5.000000
8 (Hobbit) (Lord of the Rings) 0.12 1.000000 5.000000
9 (Hunger Games) (Twilight) 0.20 0.416667 1.488095
10 (Twilight) (Hunger Games) 0.20 0.714286 1.488095
11 (Hunger Games, Harry Potter) (Twilight) 0.08 0.333333 1.190476
12 (Twilight, Harry Potter) (Hunger Games) 0.08 0.666667 1.388889
13 (Hunger Games, Twilight) (Harry Potter) 0.08 0.400000 1.000000
The metrics vary widely. Rule 4 (Percy Jackson to Harry Potter) has perfect confidence but low support (only 8% of transactions). Rule 8 (Hobbit to Lord of the Rings) shows exceptional lift at 5.0, indicating a strong association. Rules 5 and 6 have weak lift values just above 1.0, suggesting these items barely associate beyond random chance.
Applying Multi-Metric Filtering
Now filter using all three metrics at once. Each threshold serves a specific purpose in identifying valuable rules.
filtered_rules = rules[
(rules['support'] >= 0.12) &
(rules['confidence'] >= 0.50) &
(rules['lift'] >= 1.2)
]
print(f"\nFiltered to {len(filtered_rules)} high-quality rules")
print("\nFiltered rules:")
print(filtered_rules[['antecedents', 'consequents', 'support',
'confidence', 'lift']].sort_values('lift',
ascending=False).to_string())
This reduces 14 rules to 5 high-quality patterns.
Filtered to 5 high-quality rules
Filtered rules:
antecedents consequents support confidence lift
7 (Lord of the Rings) (Hobbit) 0.12 0.600000 5.000000
8 (Hobbit) (Lord of the Rings) 0.12 1.000000 5.000000
10 (Twilight) (Hunger Games) 0.20 0.714286 1.488095
2 (Hunger Games) (Harry Potter) 0.24 0.500000 1.250000
3 (Harry Potter) (Hunger Games) 0.24 0.600000 1.250000
The filtered results reveal strong patterns. The Hobbit and Lord of the Rings pair shows the highest lift (5.0), with perfect confidence when predicting Lord of the Rings from Hobbit purchases. The Harry Potter and Hunger Games combination appears in 24% of transactions with solid bidirectional confidence scores. These five rules passed all three quality tests, making them reliable for business decisions.
Interpreting Filtered Results
Each filtered rule represents an actionable pattern. The Hobbit to Lord of the Rings rule (lift 5.0, confidence 1.0) means every customer who bought Hobbit also bought Lord of the Rings, which makes perfect sense for a fantasy series and suggests bundling these titles together.
The Harry Potter and Hunger Games rules show moderate but solid associations. With 24% support and 50–60% confidence, these represent your strongest mainstream young adult fiction patterns. Twilight to Hunger Games (20% support, 71% confidence) shows customers buying vampire romance often also purchase dystopian fiction.
Compare these with the nine rules that didn’t pass filtering. The Percy Jackson to Harry Potter rule had perfect confidence but only 8% support (too rare for broad strategies). The Harry Potter to Twilight rule had adequate support (12%) but weak confidence (30%) and barely positive lift (1.07), suggesting these items don’t meaningfully associate.
Choosing Threshold Values
Your thresholds depend on business context. Test different combinations to find what works for your data.
threshold_sets = [
{'support': 0.10, 'confidence': 0.40, 'lift': 1.1},
{'support': 0.12, 'confidence': 0.50, 'lift': 1.2},
{'support': 0.15, 'confidence': 0.60, 'lift': 1.3}
]
for thresholds in threshold_sets:
filtered = rules[
(rules['support'] >= thresholds['support']) &
(rules['confidence'] >= thresholds['confidence']) &
(rules['lift'] >= thresholds['lift'])
]
print(f"Support≥{thresholds['support']}, Confidence≥{thresholds['confidence']}, "
f"Lift≥{thresholds['lift']}: {len(filtered)} rules")
The comparison shows how threshold adjustments affect results.
Support≥0.1, Confidence≥0.4, Lift≥1.1: 7 rules Support≥0.12, Confidence≥0.5, Lift≥1.2: 5 rules Support≥0.15, Confidence≥0.6, Lift≥1.3: 1 rule
Loosening thresholds to 0.10/0.40/1.1 gives you 7 rules, including some marginal patterns. Tightening to 0.15/0.60/1.3 leaves only the single strongest rule (likely Hobbit to Lord of the Rings). The middle ground at 0.12/0.50/1.2 provides 5 actionable rules without overwhelming noise.
High-volume retailers might use stricter thresholds to focus on the strongest patterns. Specialty stores with diverse catalogs might use looser thresholds to capture niche but valuable combinations. Start with moderate settings and adjust based on how many rules you need to act on.
From Filtering to Action
Filtered rules become business decisions. The Hobbit and Lord of the Rings pair (lift 5.0) deserves prominent co-placement and bundle pricing. Harry Potter and Hunger Games rules (24% support, strong bidirectional confidence) should drive recommendation algorithms and promotional campaigns targeting young adult readers.
The multi-metric approach confirms you’re working with reliable, actionable patterns rather than statistical artifacts or rare coincidences. These five filtered rules represent purchasing behaviors you can use through product placement, promotional campaigns, and personalized recommendations.
