AssociationRules

Association Rules with Python

Check out the data

Import Libraries

In [1]:
import pandas as pd
from apyori import apriori
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-aad82b729f89> in <module>
      1 import pandas as pd
----> 2 from apyori import apriori

ModuleNotFoundError: No module named 'apyori'

Check out the Data

In [ ]:
groceries = pd.read_csv('Groceries.csv', header = None)
pd.set_option('display.max_columns', 8)
In [ ]:
groceries.head()
In [ ]:
transactions = [[product for product in transaction if not pd.isnull(product)] for transaction in groceries.values]

Apriori

In [30]:
rules = list(apriori(transactions, min_support=0.004,  min_confidence=0.7, min_lift=1.07, min_length=2))

Apriori output

In [31]:
for item in rules:
    pair = item[2][0]
    base = [x for x in pair.items_base]
    add = [x for x in pair.items_add]
    base_str = ', '.join(base)
    add_str = ', '.join(add)
    print('({}) ==> ({}) sup = {}, conf = {}, lift = {}'.format(base_str, add_str, str(round(item[1],3)), str(round(item[2][0][2],3)), str(round(item[2][0][3],3))))
(curd, butter) ==> (whole milk) sup = 0.005, conf = 0.716, lift = 2.804
(curd, domestic eggs) ==> (whole milk) sup = 0.005, conf = 0.734, lift = 2.874
(citrus fruit, root vegetables, tropical fruit) ==> (other vegetables) sup = 0.004, conf = 0.786, lift = 4.061
(root vegetables, yogurt, tropical fruit) ==> (whole milk) sup = 0.006, conf = 0.7, lift = 2.74
(whipped/sour cream, tropical fruit, yogurt) ==> (whole milk) sup = 0.004, conf = 0.705, lift = 2.759
In [32]:
df = pd.read_excel('Transactions.xlsx')
df.head(11)
Out[32]:
Id Transaction Product
0 1 12383 Pill
1 2 12563 Soap
2 3 12635 Mirror
3 4 12782 Pens
4 5 12824 Pencils
5 6 12824 Sweets
6 7 12905 Tooth-paste
7 8 12938 Perfume
8 9 12944 Magazine
9 10 12944 Tooth-paste
10 11 12944 Perfume
In [33]:
transactions = df.groupby(['Transaction'])['Product'].apply(list).values.tolist()
transactions[:10]
Out[33]:
[['Pill'],
 ['Soap'],
 ['Mirror'],
 ['Pens'],
 ['Pencils', 'Sweets'],
 ['Tooth-paste'],
 ['Perfume'],
 ['Magazine', 'Tooth-paste', 'Perfume'],
 ['Pill', 'Magazine'],
 ['Magazine', 'Pencils', 'Sweets', 'Card']]
In [34]:
rules = list(apriori(transactions, min_support=0.004,  min_confidence=0.7, min_lift=1.07, min_length=2))
In [35]:
for item in rules:
    pair = item[2][0]
    base = [x for x in pair.items_base]
    add = [x for x in pair.items_add]
    base_str = ', '.join(base)
    add_str = ', '.join(add)
    print('({}) ==> ({}) sup = {}, conf = {}, lift = {}'.format(base_str, add_str, str(round(item[1],3)), str(round(item[2][0][2],3)), str(round(item[2][0][3],3))))
(Magazine, Pencils) ==> (Card) sup = 0.013, conf = 1.0, lift = 5.704
(Flash-Card, Perfume) ==> (Magazine) sup = 0.006, conf = 1.0, lift = 5.5
(Flash-Card, Mirror) ==> (Sweets) sup = 0.006, conf = 1.0, lift = 6.16
(Flash-Card, Mirror) ==> (Tooth-Brush) sup = 0.006, conf = 1.0, lift = 17.111
(Sweets, Flash-Card) ==> (Tooth-Brush) sup = 0.006, conf = 1.0, lift = 17.111
(Mirror, Perfume) ==> (Magazine) sup = 0.006, conf = 1.0, lift = 5.5
(Magazine, Shampoo) ==> (Mirror) sup = 0.006, conf = 1.0, lift = 17.111
(Magazine, Tooth-Brush) ==> (Mirror) sup = 0.006, conf = 1.0, lift = 17.111
(Magazine, Shampoo) ==> (Perfume) sup = 0.006, conf = 1.0, lift = 17.111
(Perfume, Tooth-paste) ==> (Magazine) sup = 0.006, conf = 1.0, lift = 5.5
(Mirror, Perfume) ==> (Shampoo) sup = 0.006, conf = 1.0, lift = 30.8
(Sweets, Mirror) ==> (Tooth-Brush) sup = 0.006, conf = 1.0, lift = 17.111
(Tooth-paste, Pens) ==> (Sweets) sup = 0.006, conf = 1.0, lift = 6.16
(Sweets, Magazine, Pencils) ==> (Card) sup = 0.006, conf = 1.0, lift = 5.704
(Pencils, Card, Tooth-paste) ==> (Sweets) sup = 0.006, conf = 1.0, lift = 6.16
(Sweets, Flash-Card, Mirror) ==> (Tooth-Brush) sup = 0.006, conf = 1.0, lift = 17.111
(Magazine, Mirror, Perfume) ==> (Shampoo) sup = 0.006, conf = 1.0, lift = 30.8
In [ ]: