visualizing association rule with python

Solutions on MaxInterview for visualizing association rule with python by the best coders in the world

showing results for - "visualizing association rule with python"
Samuel
25 Sep 2016
1dataset = [['Milk', 'Onion', 'Nutmeg', 'Kidney Beans', 'Eggs', 'Yogurt'],
2           ['Dill', 'Onion', 'Nutmeg', 'Kidney Beans', 'Eggs', 'Yogurt'],
3           ['Milk', 'Apple', 'Kidney Beans', 'Eggs'],
4           ['Milk', 'Unicorn', 'Corn', 'Kidney Beans', 'Yogurt'],
5           ['Corn', 'Onion', 'Onion', 'Kidney Beans', 'Ice cream', 'Eggs']]
6            
7            
8import pandas as pd
9from mlxtend.preprocessing import OnehotTransactions
10from mlxtend.frequent_patterns import apriori
11 
12oht = OnehotTransactions()
13oht_ary = oht.fit(dataset).transform(dataset)
14df = pd.DataFrame(oht_ary, columns=oht.columns_)
15print (df)           
16 
17frequent_itemsets = apriori(df, min_support=0.6, use_colnames=True)
18print (frequent_itemsets)
19 
20association_rules(frequent_itemsets, metric="confidence", min_threshold=0.7)
21rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1.2)
22print (rules)
23 
24"""
25Below is the output
26    support                     itemsets
270       0.8                       [Eggs]
281       1.0               [Kidney Beans]
292       0.6                       [Milk]
303       0.6                      [Onion]
314       0.6                     [Yogurt]
325       0.8         [Eggs, Kidney Beans]
336       0.6                [Eggs, Onion]
347       0.6         [Kidney Beans, Milk]
358       0.6        [Kidney Beans, Onion]
369       0.6       [Kidney Beans, Yogurt]
3710      0.6  [Eggs, Kidney Beans, Onion]
38 
39             antecedants            consequents  support  confidence  lift
400  (Kidney Beans, Onion)                 (Eggs)      0.6        1.00  1.25
411   (Kidney Beans, Eggs)                (Onion)      0.8        0.75  1.25
422                (Onion)   (Kidney Beans, Eggs)      0.6        1.00  1.25
433                 (Eggs)  (Kidney Beans, Onion)      0.8        0.75  1.25
444                (Onion)                 (Eggs)      0.6        1.00  1.25
455                 (Eggs)                (Onion)      0.8        0.75  1.25
46 
47"""
48