Association 3 Apriori Own Dataset
Dataset taken :- Travel.csv
Code
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from mlxtend.frequent_patterns import apriori, association_rules
from mlxtend.preprocessing import TransactionEncoder
data = pd.read_csv("travel1.csv")
if "Traveler_ID" in data.columns:
    data = data.drop(columns=["Traveler_ID"])
if "Age" in data.columns:
    data["Age_Group"] = pd.cut(
        data["Age"],
        bins=[0, 25, 40, 60, 100],
        labels=["Age_18-25", "Age_26-40", "Age_41-60", "Age_60+"],
    )
    data = data.drop(columns=["Age"])
if "Average_Spend_USD" in data.columns:
    data["Spend_Level"] = pd.qcut(
        data["Average_Spend_USD"],
        q=3,
        labels=["Spend_Low", "Spend_Medium", "Spend_High"],
    )
    data = data.drop(columns=["Average_Spend_USD"])
if "Travel_Frequency_per_Year" in data.columns:
    data["Freq_Level"] = pd.qcut(
        data["Travel_Frequency_per_Year"],
        q=3,
        labels=["Freq_Low", "Freq_Medium", "Freq_High"],
        duplicates="drop",
    )
    data = data.drop(columns=["Travel_Frequency_per_Year"])
for col in data.select_dtypes(include=["object", "category"]).columns:
    data[col] = col + "_" + data[col].astype(str)
transactions = data.values.tolist()
transactions = [
    [str(item) for item in row if pd.notna(item) and item != "nan"]
    for row in transactions
]
te = TransactionEncoder()
te_ary = te.fit(transactions).transform(transactions)
df = pd.DataFrame(te_ary, columns=te.columns_)
print("Encoded Dataset Shape:", df.shape)
frequent_itemsets = apriori(df, min_support=0.05, use_colnames=True)
print(f"Total Frequent Itemsets Generated: {len(frequent_itemsets)}")
# Plot Top Itemsets
plt.figure(figsize=(12, 6))
top_itemsets = frequent_itemsets.nlargest(n=15, columns="support").copy()
top_itemsets["itemsets_str"] = top_itemsets["itemsets"].apply(
    lambda x: ", ".join(list(x))
)
sns.barplot(
    x="itemsets_str",
    y="support",
    data=top_itemsets,
    palette="Blues_r",
    hue="itemsets_str",
    legend=False,
)
plt.xticks(rotation=45, ha="right")
plt.title("Top 15 Itemsets by Support")
plt.tight_layout()
plt.show()
# 6. Generate and Filter Association Rules
rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1.0)
if not rules.empty:
    rules["antecedent_len"] = rules["antecedents"].apply(len)
    rules["consequent_len"] = rules["consequents"].apply(len)
    # Convert frozensets to readable strings for display
    display_rules = rules.copy()
    display_rules["antecedents"] = display_rules["antecedents"].apply(
        lambda x: list(x)
    )
    display_rules["consequents"] = display_rules["consequents"].apply(
        lambda x: list(x)
    )
    print("\n--- ALL VALID RULES (Sorted by Lift) ---")
    print(
        display_rules.sort_values(by="lift", ascending=False)[
            ["antecedents", "consequents", "support", "confidence", "lift"]
        ].head(10)
    )
    print("\n--- MULTI-CONDITION RULES (Antecedent Len >= 2 & Conf > 0.3) ---")
    filtered_rules_1 = display_rules[
        (display_rules["antecedent_len"] >= 2)
        & (display_rules["confidence"] > 0.3)
        & (display_rules["lift"] > 1.0)
    ].sort_values(by=["lift", "support"], ascending=False)
    print(
        filtered_rules_1[
            ["antecedents", "consequents", "support", "confidence", "lift"]
        ].head(10)
    )
    print("\n--- HIGH-CONFIDENCE ACCURACY METRICS ---")
    high_conf_rules = association_rules(
        frequent_itemsets, metric="confidence", min_threshold=0.5
    )
    high_conf_rules["antecedents"] = high_conf_rules["antecedents"].apply(
        lambda x: list(x)
    )
    high_conf_rules["consequents"] = high_conf_rules["consequents"].apply(
        lambda x: list(x)
    )
    print(
        high_conf_rules.sort_values(by="confidence", ascending=False)[
            [
                "antecedents",
                "consequents",
                "support",
                "confidence",
                "lift",
                "leverage",
                "conviction",
            ]
        ].head(10)
    )
else:
    print(
        "No association rules met the threshold. Consider lowering min_support."
    )
Output:-