<jupyter_start><jupyter_text>Load Data<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

groceries = pd.read_csv("data\groceries.csv")
pd.set_option('display.max_rows', 100)
display(groceries)<jupyter_output><empty_output><jupyter_text>Clean<jupyter_code>purchases_frequency_df = groceries["Items"].value_counts()
purchases_frequency_df = pd.DataFrame(pd.concat([pd.Series(purchases_frequency_df.index), 
                                                 pd.Series(purchases_frequency_df.values)], axis=1))
purchases_frequency_df.columns = ["Items Purchased", "Frequency"]

purchases_df = pd.DataFrame(groceries["Items"])
purchases_df.columns = ["Items Purchased"]
groceries = groceries.drop(columns="Items")
groceries = groceries.fillna("none")<jupyter_output><empty_output><jupyter_text>EDA<jupyter_code>unique_items = []
items_frequency = []
for col in groceries:
    col_items = groceries[col].unique()
    all_items = groceries[col]
    for itm in col_items:
        unique_items.append(itm)
    for all_itms in all_items:
        items_frequency.append(all_itms)
unique_items = list(set(unique_items))
unique_items.remove("none")
print("Unique Items:", len(unique_items))
items_frequency = pd.Series([val for val in items_frequency if val != "none"])
items_frequency = items_frequency.value_counts()

items_frequency_df = pd.DataFrame(pd.concat([pd.Series(items_frequency.index), pd.Series(items_frequency.values)], axis=1))
items_frequency_df.columns = ["Item", "Frequency"]

display(items_frequency)
print(f"Total # of items purchased: {items_frequency.sum()}")
print(f"{items_frequency.index[0]} was purchased the most ({items_frequency.max()} purchases)")
print()

print(f"Mean # of times each item was purchased: {items_frequency.mean().round(0)}")
print(f"Stdandard Deviation of # of times each item was purchased: {items_frequency.std().round(0)}")
print()

print(f"Mean # of items purchased per customer: {purchases_df.mean()}")
print(f"Standard Deviation of # of items purchased per customer: {purchases_df.std()}")
print()

print(f"Min # of items purchased per customer: {purchases_df.min()}")
print(f"Max # of items purchased per customer: {purchases_df.max()}")
plt.figure(figsize=(14,8))
plt.bar(items_frequency[:30].index, items_frequency[:30], color="teal")
plt.xticks(rotation=75)
plt.xlabel("Item")
plt.ylabel("Frequency")
plt.title("Item Frequency (30 most frequent items)")
plt.show()

display(items_frequency_df)
plt.figure(figsize=(14,8))
plt.bar(purchases_frequency_df["Items Purchased"], purchases_frequency_df["Frequency"])
plt.xlabel("Items")
plt.ylabel("Frequency")
plt.title("Number of Items Purchased per Transaction Frequency")
plt.show()<jupyter_output><empty_output><jupyter_text>Preprocess<jupyter_code>groceries_list = groceries.values.tolist()
cleaned_list = []

for row in groceries_list:
    new_row = [val for val in row if val != "none"]
    cleaned_list.append(new_row)
from mlxtend.preprocessing import TransactionEncoder

enc = TransactionEncoder()
enc.fit(cleaned_list)
enc_groceries = pd.DataFrame(enc.transform(cleaned_list), columns=enc.columns_)
display(enc_groceries)<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>from mlxtend.frequent_patterns import apriori, association_rules

itemset = apriori(enc_groceries, min_support=0.01, use_colnames=True)
display(itemset)
rules = association_rules(itemset, metric="lift", min_threshold=2.25)
display(rules.head(10))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13340967649584884, 'fraction_numerical': 0.00858860578299456, 'mean_word_length': 7.358851674641149, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '328998', 'license': 'MIT', 'n_tokens_mistral': 1221, 'n_tokens_neox': 1176, 'n_words': 213, 'path': 'ml_association_rule.ipynb', 'repo_name': 'Evan-Lehmann/ml-association-rule', 'size': '3493'}