Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
735 B
3
Indexable
Never
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules
import pandas as pd

# Load the dataset from CSV file
df = pd.read_csv('/content/bread basket.csv')

# Convert the items column to a one-hot encoded format
oht = df.groupby(['Transaction', 'Item'])['Item'].count().unstack().reset_index().fillna(0).set_index('Transaction')
oht = oht.applymap(lambda x: 1 if x > 0 else 0)

# Generate frequent itemsets
frequent_itemsets = apriori(oht, min_support=0.2, use_colnames=True)

# Generate association rules
rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.7)

print("Frequent itemsets:")
print(frequent_itemsets)

print("\nAssociation rules:")
print(rules)
Leave a Comment