<jupyter_start><jupyter_text>Calculate the number of items<jupyter_code>flattend_data = reduce(lambda x,y: x+y, data)
flattend_data[0:5]

print("Number of unique items:", len(set(flattend_data)))
print("The total number of item purchased over the week is:", len(flattend_data))
print("the most common items are:")
for item in Counter(flattend_data).most_common()[0:20]:
    print("   {:<22} {:>4} {:<6}".format(item[0], item[1], str(round(item[1]*100/len(flattend_data), 2))+ "%"))
    
data_sorted = sorted(data, key=len, reverse=True)
print("\nThe largest transactions:")
for i in data_sorted[0:5]:
    print("{:<2} items, {}\n".format(len(i), i))
# Maximum possible combinations.
from decimal import Decimal
'%.2E' % Decimal((3**120) - (2**121) + 1)
# The average amount of freq of each unique label.
round(np.mean(list(Counter(flattend_data).values())) / len(flattend_data), 4)
# Graph the % each item has in the entire dataset.
b = Counter(flattend_data).most_common()
b = list(map(lambda x : x[1] / len(flattend_data), b))
sns.lineplot(np.arange(len(b)), b)
rules = []
for apr_data in apr(data_sorted, 
        # Support must be larger then the mean occupation of all items.
        min_support = 0.0083, 
        # .4 allows us a decent number of rules to use while still
        # being relative strong.
        min_confidence = 0.40,
        min_lift = 0.0,
        max_length = None):
    a = set(apr_data.ordered_statistics[0].items_base)
    b = set(apr_data.ordered_statistics[0].items_add)
    c = round(apr_data.ordered_statistics[0].confidence, 4)
    support = apr_data[1]
    rules.append((a, b, c, support))

# We generate the rules and store them in a dataset
print("number of rules:", len(rules), "\n")
df = pd.DataFrame(rules, columns=["base", "add", "confidence", "support"])
df
# We plot the network we can identify from the rules.
import networkx as nx
import matplotlib.patheffects as PathEffects

options = {
    "font_color": "black",
    "font_size": 14,
    "style": "doted",
    "edge_color": "0.3",
    "node_color": "salmon"
}

G = nx.DiGraph()
plt.figure(figsize=(30,25))
for i, item in df.iterrows():
    G.add_edge(", ".join(item["base"]), ", ".join(item["add"]), weight=item["confidence"])
    
pos = nx.circular_layout(G)

#nx.draw_networkx(G, **options)

nx.draw_networkx_edges(G, pos, **options)
nx.draw_networkx_nodes(G, pos, **options)
labels = nx.draw_networkx_labels(G, pos, **options)
[label.set_path_effects([PathEffects.withStroke(linewidth=1, foreground='w')]) for label in [*labels.values()]]
print()
te = TransactionEncoder()
te_ary = te.fit(data).transform(data)
one_hot_encoded_dataset = pd.DataFrame(te_ary, columns=te.columns_)
one_hot_encoded_dataset.head()
import time

funcs = {
    "mlxtend_apriori": lambda s: apriori(one_hot_encoded_dataset, min_support=s),
    "mlxtend_apriori_fpgrowth": lambda s: fpgrowth(one_hot_encoded_dataset, min_support=s),
    "prev_apriori": lambda s: list(apr(data, min_support=s))
}
times = {name: {} for name in funcs}

for support in np.arange(0.02, 0.0004, -0.0001):
    for func_name in funcs:
        start = time.time()
        funcs[func_name](support)
        end = time.time()
        times[func_name][round(support, 6)] = end - start
pd.DataFrame(times)
plt.figure(figsize=(12.5, 7.5))

sns.lineplot(data=pd.DataFrame(times).tail(75))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15725326991676575, 'fraction_numerical': 0.022889417360285373, 'mean_word_length': 4.713073005093379, 'pattern_counts': {'":': 8, '<': 8, '<?xml version=': 0, '>': 6, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '392190', 'license': 'MIT', 'n_tokens_mistral': 1273, 'n_tokens_neox': 1220, 'n_words': 290, 'path': 'labs/2/part_2_association_rules/Untitled.ipynb', 'repo_name': 'FilipAndersson245/Data-Science', 'size': '3364'}