Skip to content

Instantly share code, notes, and snippets.

@fsndzomga
Created September 25, 2023 21:33
Show Gist options
  • Save fsndzomga/7db2e61556a2d1951d4b4d805ae86767 to your computer and use it in GitHub Desktop.
Save fsndzomga/7db2e61556a2d1951d4b4d805ae86767 to your computer and use it in GitHub Desktop.
Code for market basket analysis
import pyfpgrowth
import pandas as pd
# Assuming df is your existing DataFrame
transactions = combined_data.groupby('Invoice')['Description'].apply(list).reset_index(name='items')
# Convert the 'items' column into a list of itemsets
transactions['items'] = transactions['items'].apply(lambda x: list(set(x)))
# Create a list of transactions as lists
transaction_list = transactions['items'].tolist()
# Perform frequent itemset mining
support_threshold = 0.02
patterns = pyfpgrowth.find_frequent_patterns(transaction_list, int(len(transaction_list) * support_threshold))
# Generate association rules
confidence_threshold = 0.5
rules = pyfpgrowth.generate_association_rules(patterns, confidence_threshold)
# Convert the association rules to a DataFrame for easier analysis
rules_df = pd.DataFrame(list(rules.items()), columns=['Itemset', 'Support'])
# View the association rules
print(rules_df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment