Created
September 25, 2023 21:33
-
-
Save fsndzomga/7db2e61556a2d1951d4b4d805ae86767 to your computer and use it in GitHub Desktop.
Code for market basket analysis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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