<jupyter_start><jupyter_text>Association RulesLet's say you are a Machine Learning engineer working for a clothing company and you want to adopt new strategies to improve the company's profit.Use this dataset and the association rules mining to find new marketing plans. Note here that one of the strategies can be based on which items should be put together    dataset = [['Skirt', 'Sneakers', 'Scarf', 'Pants', 'Hat'],    ['Sunglasses', 'Skirt', 'Sneakers', 'Pants', 'Hat'],    ['Dress', 'Sandals', 'Scarf', 'Pants', 'Heels'],    ['Dress', 'Necklace', 'Earrings', 'Scarf', 'Hat', 'Heels', 'Hat'],    ['Earrings', 'Skirt', 'Skirt', 'Scarf', 'Shirt', 'Pants']]Bonus: try to do some visualization before applying the Apriori algorithm.Let's do the same checkpoint but with a bigger dataset!  Installing libraries<jupyter_code>import mlxtend
import pandas as pd
from mlxtend.preprocessing import TransactionEncoder<jupyter_output><empty_output><jupyter_text>Reading the dataset<jupyter_code>sampled_dataset = [['Skirt', 'Sneakers', 'Scarf', 'Pants', 'Hat'],

    ['Sunglasses', 'Skirt', 'Sneakers', 'Pants', 'Hat'],

    ['Dress', 'Sandals', 'Scarf', 'Pants', 'Heels'],

    ['Dress', 'Necklace', 'Earrings', 'Scarf', 'Hat', 'Heels', 'Hat'],

   ['Earrings', 'Skirt', 'Skirt', 'Scarf', 'Shirt', 'Pants']]

dataset = []
with open("Market_Basket_Optimisation.csv", "r") as file:
    for line in file.readlines():
        dataset.append(line.rstrip().split(","))
len("Number of lines in: ", dataset)


te = TransactionEncoder()
te_array = te.fit_transform(dataset)
df = pd.DataFrame(te_array, columns=te.columns_)
df.head()<jupyter_output><empty_output><jupyter_text>Visualization<jupyter_code>import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
sns.set_theme()
plt.figure(figsize=(18, 5))
LIMIT = 10
df_item_counts = df.sum(axis=0).sort_values(ascending=False)[:LIMIT]
fig = sns.barplot(x=df_item_counts.index, y=df_item_counts.values, estimator=sum, ci=None)

print(f"Top {LIMIT} items from dataset")<jupyter_output>Top 10 items from dataset<jupyter_text>Apriori Algorithm & Building Association Rules I had to lower min_support to 0.02 to produce enough frequent itemsets<jupyter_code>from mlxtend.frequent_patterns import apriori
frequent_itemsets = apriori(df, min_support=0.02, use_colnames=True).sort_values('support', ascending=False)
frequent_itemsets[:20]<jupyter_output><empty_output><jupyter_text>It seems like mineral water, eggs, and spaghetti are the strongest supporting itemsets present in the dataset<jupyter_code>from mlxtend.frequent_patterns import association_rules 
association_rules(frequent_itemsets,min_threshold=0.4, metric="confidence") # associate itemsets with confidence over 1%.<jupyter_output><empty_output><jupyter_text>ground beef and mineral water have the strongest confidence with this association rule<jupyter_code>from mlxtend.frequent_patterns import association_rules
association_rules(frequent_itemsets,metric="lift",min_threshold=1.75).sort_values(['confidence', 'lift'], ascending=False)
 # associate itemsets with confidence over 70%.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1295585412667946, 'fraction_numerical': 0.007677543186180422, 'mean_word_length': 7.36096256684492, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '629244', 'license': 'MIT', 'n_tokens_mistral': 1069, 'n_tokens_neox': 1016, 'n_words': 274, 'path': 'checkpoint9-associationrules.ipynb', 'repo_name': 'isados/gomycode', 'size': '3126'}