liveBook · Manning

Association rule mining

As explained in Outlier Detection in Python

An association rule is an implication describing how the presence of one set of categorical values is associated with another set of values.

Association rule mining describes relationships between categorical values with rules of the form X → Y. A frequent item set may span one or more columns, such as area code 416 being associated with Toronto. section 7.10

Evaluate records in two complementary ways:

  • Count how many association rules hold for each row; fewer satisfied rules indicate greater outlierness. section 7.10
  • Find exceptions where an antecedent is present but its consequent is absent, such as area code 416 without Toronto. section 7.10
  • Combine exception testing with another detector when rows containing few or no frequent item sets also matter. section 7.10

The exception-based approach is interpretable: each flagged row can be linked to rules whose left-hand side holds while the right-hand side does not. It therefore identifies a different anomaly type from methods based mainly on sparse frequent-itemset membership. section 7.10

Listing 7.12 Creating frequent item sets
from mlxtend.frequent_patterns import apriori
import pandas as pd
from sklearn.datasets import fetch_openml
import matplotlib.pyplot as seaborn
 
data = fetch_openml('SpeedDating', version=1, parser='auto')             #1
data_df = pd.DataFrame(data.data, columns=data.feature_names)
data_df = data_df[['d_pref_o_attractive', 'd_pref_o_sincere',  
                   'd_pref_o_intelligence', 'd_pref_o_funny', 
                   'd_pref_o_ambitious', 'd_pref_o_shared_interests']]   #2
data_df = pd.get_dummies(data_df)                                        #3
frequent_itemsets = apriori(data_df, min_support=0.3, use_colnames=True) #4

Create one-hot categorical columns, then mine frequent item sets with Apriori and a chosen minimum support. section 7.10

Listing 7.13 Creating and testing association rules
from mlxtend.frequent_patterns import association_rules
 
assoc_rules = association_rules(frequent_itemsets, metric="confidence",
                                min_threshold=0.7)

Generate rules from the frequent item sets by selecting confidence and a minimum threshold. section 7.10

SpeedDating categorical preferences

After encoding preference ranges, rules can associate high attractiveness preferences with low ambition preferences or low shared-interests preferences. A row that satisfies the antecedent but not the consequent becomes a rule exception.

How Outlier Detection in Python covers it

How deep it goes
Explains rule formation, two scoring perspectives, frequent-itemset mining, and record-level exception testing.
Examples
Uses telephone area codes with cities and SpeedDating preference ranges to illustrate rules, confidence, and exceptions.
What it assumes
Starts with categorical rows and uses one-hot encoded columns before mining frequent item sets.
What you can do after
Builds categorical outlier scores with mlxtend by mining item sets, generating rules, and locating antecedent-consequent exceptions.
How it connects
Connects frequent itemset mining, outlier detection, and categorical data analysis through association-rule exceptions.

Also explained in

Compare how 2 books explain association rule mining →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Outlier Detection in Python. The section links point to the parts of the book it draws on.