<jupyter_start><jupyter_text>Association Rule per Segment<jupyter_code>customer_segment = pd.read_csv("clustering_output.csv")
customer_segment.head()
for p in range(max(customer_segment['Segment'])):
    selected_customer = customer_segment['CustomerID'][customer_segment['Segment']==p]
    selected_customer = list(selected_customer.astype('str'))
    ecommerce_filtered = ecommerce[ecommerce['CustomerID'].isin(selected_customer)]
    stock_transaction_filtered = ecommerce_filtered.groupby(['InvoiceNo', 'StockCode']).size().unstack().fillna(0)
    for i in range(stock_transaction_filtered.shape[1]):
        stock_transaction_filtered.iloc[:,i][stock_transaction_filtered.iloc[:,i]>1] = 1
    stock_transaction_filtered = stock_transaction_filtered.rename(columns=stock_map1[stock_transaction_filtered.columns])
    stock_freq_item_filtered = apriori(stock_transaction_filtered, min_support=0.02, use_colnames=True)
    stock_assoc_rules_filtered = association_rules(stock_freq_item_filtered, metric="confidence", min_threshold=0.3)
    print("Segment", p, ":\n", stock_assoc_rules_filtered.sort_values(['confidence', 'support'], ascending=False).iloc[1:10,:])<jupyter_output>Segment  0 :
                             antecedants                           consequents  \
38         (ALARM CLOCK BAKELIKE IVORY)           (ALARM CLOCK BAKELIKE RED )   
30    (GREEN REGENCY TEACUP AND SAUCER)    (ROSES REGENCY TEACUP AND SAUCER )   
23  (PAPER CHAIN KIT VINTAGE CHRISTMAS)     (PAPER CHAIN KIT 50'S CHRISTMAS )   
34         (ALARM CLOCK BAKELIKE GREEN)           (ALARM CLOCK BAKELIKE RED )   
25            (JUMBO BAG PINK POLKADOT)             (JUMBO BAG RED RETROSPOT)   
36          (ALARM CLOCK BAKELIKE PINK)           (ALARM CLOCK BAKELIKE RED )   
6             (LUNCH BAG PINK POLKADOT)             (LUNCH BAG RED RETROSPOT)   
21   (RED HANGING HEART T-LIGHT HOLDER)  (CREAM HANGING HEART T-LIGHT HOLDER)   
29   (ROSES REGENCY TEACUP AND SAUCER )     (GREEN REGENCY TEACUP AND SAUCER)   

    antecedent support  consequent support   support  confidence       lift  \
38            0.029279            0.055663  0.021236    0.725275  13.029791   
30            0.0[...]
{'fraction_non_alphanumeric': 0.09419295839048926, 'fraction_numerical': 0.03429355281207133, 'mean_word_length': 3.3326732673267325, 'pattern_counts': {'":': 1, '<': 4, '<?xml version=': 0, '>': 5, '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': '89775', 'license': 'MIT', 'n_tokens_mistral': 792, 'n_tokens_neox': 724, 'n_words': 148, 'path': '01_super-unsupervised-learning/StockAssociationRules.ipynb', 'repo_name': 'jeannefukumaru/hppython', 'size': '2187'}