Surya_from_1627

USML5

Sep 27th, 2026
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. import pandas as pd
  2. from mlxtend.preprocessing import TransactionEncoder
  3. from mlxtend.frequent_patterns import fpgrowth, association_rules
  4.  
  5. # 1. Define your raw retail transaction data
  6. dataset = [
  7.     ['milk', 'bread', 'nut'],
  8.     ['milk', 'bread', 'apple'],
  9.     ['milk', 'bread', 'nut'],
  10.     ['bread', 'apple'],
  11.     ['milk', 'nut'],
  12. ]
  13.  
  14.  
  15. te = TransactionEncoder()
  16. te_ary = te.fit(dataset).transform(dataset)
  17. df = pd.DataFrame(te_ary, columns=te.columns_)
  18.  
  19. print("Encoded DataFrame Structure")
  20. print(df)
  21.  
  22. frequent_itemsets = fpgrowth(df, min_support=0.4, use_colnames=True)
  23.  
  24. print("Mined Frequent Itemsets")
  25. print(frequent_itemsets)
  26.  
  27. rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.7)
  28.  
  29. print("Generated Association Rules")
  30. print(rules[['antecedents', 'consequents', 'support', 'confidence', 'lift']])
  31.  
Advertisement
Add Comment
Please, Sign In to add comment