<jupyter_start><jupyter_text>Prefixspan<jupyter_code>series_of_all_carts = df.groupby(['CustomerID'])['BasketID'].unique().apply(list)
series_of_all_carts # get the series
# get the list of carts for a customer having more than 10 carts 
list_of_all_carts = []
for list_of_carts_by_user in series_of_all_carts:
    if len(list_of_carts_by_user) > threshold:
        list_of_all_carts.append(list(list_of_carts_by_user))

len(list_of_all_carts)
# list_of_all_carts
import itertools
from prefixspan import PrefixSpan
db = []

for list_of_customer_basket_id in list_of_all_carts:
    all_items_for_users = []
    for basketID in list_of_customer_basket_id:
        all_items_for_users.append(df[df['BasketID']== basketID]['ProdDescr'].values.tolist())
    all_items_for_users = list(itertools.chain(*all_items_for_users))
    db.append(all_items_for_users)
ps = PrefixSpan(db)
result = ps.frequent(support)
result
# print(ps.topk(100))
parameters = f'Support: {support}, Threshold: {threshold} \n'
print_result = f'{result} \n'
# Open a file with access mode 'a'
with open("results.txt", "a") as file_object:
    # Append 'hello' at the end of file
    file_object.write(parameters)
    file_object.write(print_result)
len(result)
# size_l1 = 0
list_1 = list_2 = list_3 = list_4 = list_5 = list_6 = list_7 = list_8 = []
best = best1 = best2 = best3 = best4 = best5 = best6 = best7 = best8 = best9 = best10 = 0
best_rule = best1_rule = best2_rule = best3_rule = best4_rule = best5_rule = best6_rule = best7_rule = best8_rule = best9_rule = best10_rule = ()
best_distinct_2 = best_distinct_3 = best_distinct_4 = best_distinct_5 = best_distinct_6 = best_distinct_7 = best_distinct_8 = 0
best_distinct_2_rule = best_distinct_3_rule = best_distinct_4_rule = best_distinct_5_rule = best_distinct_6_rule = best_distinct_7_rule = best_distinct_8_rule = ()

for rule in result:
#     if rule[0] > best:
#         best = rule[0]
#         best_rule = rule
#     print(rule)
    if (len(rule[1]) == 1): #if rule[0] > 200:
#         size_l1 = size_l1 + 1
#         list_1.append(rule)
        if rule[0] > best1:
            best1 = rule[0]
            best1_rule = rule
    if (len(rule[1]) == 2):
        if rule[0] > best2:
            best2 = rule[0]
            best2_rule = rule
            list_2.append(rule)
    if (len(rule[1]) == 3):
        if rule[0] > best3:
            best3 = rule[0]
            best3_rule = rule
            list_3.append(rule)
    if (len(rule[1]) == 4):
        if rule[0] > best4:
            best4 = rule[0]
            best4_rule = rule
            list_4.append(rule)
    if (len(rule[1]) == 5):
        if rule[0] > best5:
            best5 = rule[0]
            best5_rule = rule
            list_5.append(rule)
    if (len(rule[1]) == 6):
        if rule[0] > best6:
            best6 = rule[0]
            best6_rule = rule
            list_6.append(rule)
    if (len(rule[1]) == 7):
        if rule[0] > best7:
            best7 = rule[0]
            best7_rule = rule
            list_7.append(rule)
    if (len(rule[1]) == 8):
        if rule[0] > best8:
            best8 = rule[0]
            best8_rule = rule
            list_8.append(rule)
    if (len(rule[1]) == 9):
        if rule[0] > best9:
            best9 = rule[0]
            best9_rule = rule
    if (len(rule[1]) == 10):
        if rule[0] > best10:
            best10 = rule[0]
            best10_rule = rule
            
    if (len(rule[1]) == 2) and (rule[1][0] != rule[1][1]):
        if rule[0] > best_distinct_2:
            best_distinct_2 = rule[0]
            best_distinct_2_rule = rule
            
    if (len(rule[1]) == 3) and (rule[1][0] != rule[1][1]) and (rule[1][0] != rule[1][2]) and (rule[1][1] != rule[1][2]):
        if rule[0] > best_distinct_3:
            best_distinct_3 = rule[0]
            best_distinct_3_rule = rule
            
    if (len(rule[1]) == 4) and (rule[1][0] != rule[1][1]) and (rule[1][0] != rule[1][2]) and (rule[1][1] != rule[1][2]) and (rule[1][0] != rule[1][3]) and (rule[1][1] != rule[1][3]) and (rule[1][2] != rule[1][3]):
        if rule[0] > best_distinct_4:
            best_distinct_4 = rule[0]
            best_distinct_4_rule = rule
    
    if (len(rule[1]) == 5) and (rule[1][0] != rule[1][1]) and (rule[1][0] != rule[1][2]) and (rule[1][1] != rule[1][2]) and (rule[1][0] != rule[1][3]) and (rule[1][1] != rule[1][3]) and (rule[1][2] != rule[1][3]) and (rule[1][0] != rule[1][4]) and (rule[1][1] != rule[1][4]) and (rule[1][2] != rule[1][4]) and (rule[1][3] != rule[1][4]):
        if rule[0] > best_distinct_5:
            best_distinct_5 = rule[0]
            best_distinct_5_rule = rule
    
    if (len(rule[1]) == 6) and (rule[1][0] != rule[1][1]) and (rule[1][0] != rule[1][2]) and (rule[1][1] != rule[1][2]) and (rule[1][0] != rule[1][3]) and (rule[1][1] != rule[1][3]) and (rule[1][2] != rule[1][3]) and (rule[1][0] != rule[1][4]) and (rule[1][1] != rule[1][4]) and (rule[1][2] != rule[1][4]) and (rule[1][3] != rule[1][4]) and (rule[1][0] != rule[1][5]) and (rule[1][1] != rule[1][5]) and (rule[1][2] != rule[1][5]) and (rule[1][3] != rule[1][5]) and (rule[1][4] != rule[1][5]):
        if rule[0] > best_distinct_6:
            best_distinct_6 = rule[0]
            best_distinct_6_rule = rule

#print(f'Best rule: {best_rule}')            
print(f'Best 1 rule: {best1_rule}')
print(f'Best 2 rule: {best2_rule}')
print(f'Best 3 rule: {best3_rule}')
print(f'Best 4 rule: {best4_rule}')
print(f'Best 5 rule: {best5_rule}')
print(f'Best 6 rule: {best6_rule}')
print(f'Best 7 rule: {best7_rule}')
print(f'Best 8 rule: {best8_rule}')
print(f'Best 9 rule: {best9_rule}')
print(f'Best 10 rule: {best10_rule}')

print(f'Best distinct 2 rule: {best_distinct_2_rule}')
print(f'Best distinct 3 rule: {best_distinct_3_rule}')
print(f'Best distinct 4 rule: {best_distinct_4_rule}')
print(f'Best distinct 5 rule: {best_distinct_5_rule}')
print(f'Best distinct 6 rule: {best_distinct_6_rule}')
print(f'Best distinct 7 rule: {best_distinct_7_rule}')
print(f'Best distinct 8 rule: {best_distinct_8_rule}')<jupyter_output>Best 1 rule: (295, ['REGENCY CAKESTAND 3 TIER'])
Best 2 rule: (198, ['WHITE HANGING HEART T-LIGHT HOLDER', 'WHITE HANGING HEART T-LIGHT HOLDER'])
Best 3 rule: (142, ['WHITE HANGING HEART T-LIGHT HOLDER', 'WHITE HANGING HEART T-LIGHT HOLDER', 'WHITE HANGING HEART T-LIGHT HOLDER'])
Best 4 rule: (108, ['WHITE HANGING HEART T-LIGHT HOLDER', 'WHITE HANGING HEART T-LIGHT HOLDER', 'WHITE HANGING HEART T-LIGHT HOLDER', 'WHITE HANGING HEART T-LIGHT HOLDER'])
Best 5 rule: (78, ['WHITE HANGING HEART T-LIGHT HOLDER', 'WHITE HANGING HEART T-LIGHT HOLDER', 'WHITE HANGING HEART T-LIGHT HOLDER', 'WHITE HANGING HEART T-LIGHT HOLDER', 'WHITE HANGING HEART T-LIGHT HOLDER'])
Best 6 rule: (59, ['WHITE HANGING HEART T-LIGHT HOLDER', 'WHITE HANGING HEART T-LIGHT HOLDER', 'WHITE HANGING HEART T-LIGHT HOLDER', 'WHITE HANGING HEART T-LIGHT HOLDER', 'WHITE HANGING HEART T-LIGHT HOLDER', 'WHITE HANGING HEART T-LIGHT HOLDER'])
Best 7 rule: (43, ['JUMBO BAG RED RETROSPOT', 'JUMBO BAG PINK POLKADOT', 'JUMBO BAG RED [...]
{'fraction_non_alphanumeric': 0.16454802259887005, 'fraction_numerical': 0.053813559322033896, 'mean_word_length': 3.3176829268292685, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 22, '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': '418875', 'license': 'MIT', 'n_tokens_mistral': 2894, 'n_tokens_neox': 2815, 'n_words': 637, 'path': 'DM_Group16_TASK4/SPM_Prefixspan.ipynb', 'repo_name': 'luca-demartino/data-mining-project', 'size': '7080'}