Apriori And Fp-Growth Algorithm
Association Rules
S.Reza.Eftekhary 1400/11/12
1 - Importing the required libraries
2 - Loading and exploring the data
Import Data UCI
http://archive.ics.uci.edu/ml/datasets/Online+Retail
0
536365
85123A
1
536365
71053
2
536365
84406B
3
536365
84029G
4
536365
84029E
3 - Exploring the columns of the data
Index(['InvoiceNo', 'StockCode', 'Description', 'Quantity', 'InvoiceDate',
'UnitPrice', 'CustomerID', 'Country'],
dtype='object')
4 - Exploring the different regions of transactions
0
12346
['United Kingdom', 'United Kingdom']
1
12347
['Iceland', 'Iceland', 'Iceland', 'Iceland', 'Iceland', 'Iceland', 'Iceland', 'Iceland', 'Iceland',…
2
12348
['Finland', 'Finland', 'Finland', 'Finland', 'Finland', 'Finland', 'Finland', 'Finland', 'Finland',…
3
12349
['Italy', 'Italy', 'Italy', 'Italy', 'Italy', 'Italy', 'Italy', 'Italy', 'Italy', 'Italy', 'Italy',…
4
12350
['Norway', 'Norway', 'Norway', 'Norway', 'Norway', 'Norway', 'Norway', 'Norway', 'Norway', 'Norway'…
5
12352
['Norway', 'Norway', 'Norway', 'Norway', 'Norway', 'Norway', 'Norway', 'Norway', 'Norway', 'Norway'…
6
12353
['Bahrain', 'Bahrain', 'Bahrain', 'Bahrain']
7
12354
['Spain', 'Spain', 'Spain', 'Spain', 'Spain', 'Spain', 'Spain', 'Spain', 'Spain', 'Spain', 'Spain',…
8
12355
['Bahrain', 'Bahrain', 'Bahrain', 'Bahrain', 'Bahrain', 'Bahrain', 'Bahrain', 'Bahrain', 'Bahrain',…
9
12356
['Portugal', 'Portugal', 'Portugal', 'Portugal', 'Portugal', 'Portugal', 'Portugal', 'Portugal', 'P…
5 - Cleaning the Data
6 - Splitting the data according to the region of transaction
7 - Hot encoding the Data
8 - Building the models and analyzing the results
a) France:
antecedents \
44 (JUMBO BAG WOODLAND ANIMALS)
260 (PLASTERS IN TIN CIRCUS PARADE, RED TOADSTOOL ...
272 (PLASTERS IN TIN WOODLAND ANIMALS, RED TOADSTO...
301 (SET/6 RED SPOTTY PAPER CUPS, SET/20 RED RETRO...
302 (SET/6 RED SPOTTY PAPER PLATES, SET/20 RED RET...
consequents antecedent support consequent support \
44 (POSTAGE) 0.076531 0.765306
260 (POSTAGE) 0.051020 0.765306
272 (POSTAGE) 0.053571 0.765306
301 (SET/6 RED SPOTTY PAPER PLATES) 0.102041 0.127551
302 (SET/6 RED SPOTTY PAPER CUPS) 0.102041 0.137755
support confidence lift leverage conviction
44 0.076531 1.000 1.306667 0.017961 inf
260 0.051020 1.000 1.306667 0.011974 inf
272 0.053571 1.000 1.306667 0.012573 inf
301 0.099490 0.975 7.644000 0.086474 34.897959
302 0.099490 0.975 7.077778 0.085433 34.489796
44
frozenset({'JUMBO BAG WOODLAND ANIMALS'})
frozenset({'POSTAGE'})
260
frozenset({'PLASTERS IN TIN CIRCUS PARADE', 'RED TOADSTOOL LED NIGHT LIGHT'})
frozenset({'POSTAGE'})
272
frozenset({'PLASTERS IN TIN WOODLAND ANIMALS', 'RED TOADSTOOL LED NIGHT LIGHT'})
frozenset({'POSTAGE'})
301
frozenset({'SET/6 RED SPOTTY PAPER CUPS', 'SET/20 RED RETROSPOT PAPER NAPKINS'})
frozenset({'SET/6 RED SPOTTY PAPER PLATES'})
302
frozenset({'SET/6 RED SPOTTY PAPER PLATES', 'SET/20 RED RETROSPOT PAPER NAPKINS'})
frozenset({'SET/6 RED SPOTTY PAPER CUPS'})
336
frozenset({'SET/6 RED SPOTTY PAPER CUPS', 'POSTAGE', 'SET/20 RED RETROSPOT PAPER NAPKINS'})
frozenset({'SET/6 RED SPOTTY PAPER PLATES'})
337
frozenset({'SET/6 RED SPOTTY PAPER PLATES', 'POSTAGE', 'SET/20 RED RETROSPOT PAPER NAPKINS'})
frozenset({'SET/6 RED SPOTTY PAPER CUPS'})
129
frozenset({'RED RETROSPOT PICNIC BAG'})
frozenset({'POSTAGE'})
143
frozenset({'SET OF 9 BLACK SKULL BALLOONS'})
frozenset({'POSTAGE'})
175
frozenset({'SET/6 RED SPOTTY PAPER PLATES'})
frozenset({'SET/6 RED SPOTTY PAPER CUPS'})
88
frozenset({'PACK OF 6 SKULL PAPER CUPS'})
frozenset({'POSTAGE'})
134
frozenset({'RETROSPOT PARTY BAG + STICKER SET'})
frozenset({'POSTAGE'})
34
frozenset({'GUMBALL COAT RACK'})
frozenset({'POSTAGE'})
91
frozenset({'PACK OF 6 SKULL PAPER PLATES'})
frozenset({'POSTAGE'})
296
frozenset({'SET/6 RED SPOTTY PAPER PLATES', 'POSTAGE'})
frozenset({'SET/6 RED SPOTTY PAPER CUPS'})
36
frozenset({'JAM MAKING SET PRINTED'})
frozenset({'POSTAGE'})
160
frozenset({'TEA PARTY BIRTHDAY CARD'})
frozenset({'POSTAGE'})
159
frozenset({'STRAWBERRY LUNCH BOX WITH CUTLERY'})
frozenset({'POSTAGE'})
138
frozenset({'ROUND SNACK BOXES SET OF4 WOODLAND'})
frozenset({'POSTAGE'})
25
frozenset({'CHILDRENS CUTLERY SPACEBOY'})
frozenset({'CHILDRENS CUTLERY DOLLY GIRL'})
17
frozenset({'ASSORTED COLOUR MINI CASES'})
frozenset({'POSTAGE'})
140
frozenset({'SET OF 2 TEA TOWELS APPLE AND PEARS'})
frozenset({'POSTAGE'})
205
frozenset({'LUNCH BAG APPLE DESIGN', 'LUNCH BAG RED RETROSPOT'})
frozenset({'POSTAGE'})
235
frozenset({'LUNCH BOX WITH CUTLERY RETROSPOT', 'STRAWBERRY LUNCH BOX WITH CUTLERY'})
frozenset({'POSTAGE'})
277
frozenset({'ROUND SNACK BOXES SET OF4 WOODLAND', 'ROUND SNACK BOXES SET OF 4 FRUITS'})
frozenset({'POSTAGE'})
From the above output, it can be seen that paper cups and paper and plates are bought together in France. This is because the French have a culture of having a get-together with their friends and family atleast once a week. Also, since the French government has banned the use of plastic in the country, the people have to purchase the paper-based alternatives.
b) United Kingdom:
If the rules for British transactions are analyzed a little deeper, it is seen that the British people buy different colored tea-plates together. A reason behind this may be because typically the British enjoy tea very much and often collect different colored tea-plates for different occasions.
c) Portugal:
antecedents consequents \
1170 (SET 12 COLOUR PENCILS DOLLY GIRL) (SET 12 COLOUR PENCILS SPACEBOY)
1171 (SET 12 COLOUR PENCILS SPACEBOY) (SET 12 COLOUR PENCILS DOLLY GIRL)
1172 (SET OF 4 KNICK KNACK TINS LONDON) (SET 12 COLOUR PENCILS DOLLY GIRL)
1173 (SET 12 COLOUR PENCILS DOLLY GIRL) (SET OF 4 KNICK KNACK TINS LONDON)
1174 (SET OF 4 KNICK KNACK TINS POPPIES) (SET 12 COLOUR PENCILS DOLLY GIRL)
antecedent support consequent support support confidence lift \
1170 0.051724 0.051724 0.051724 1.0 19.333333
1171 0.051724 0.051724 0.051724 1.0 19.333333
1172 0.051724 0.051724 0.051724 1.0 19.333333
1173 0.051724 0.051724 0.051724 1.0 19.333333
1174 0.051724 0.051724 0.051724 1.0 19.333333
leverage conviction
1170 0.049049 inf
1171 0.049049 inf
1172 0.049049 inf
1173 0.049049 inf
1174 0.049049 inf
On analyzing the association rules for Portuguese transactions, it is observed that Tiffin sets (Knick Knack Tins) and color pencils. These two products typically belong to a primary school going kid. These two products are required by children in school to carry their lunch and for creative work respectively and hence are logically make sense to be paired together.
d) Sweden:
antecedents consequents \
0 (12 PENCILS SMALL TUBE SKULL) (PACK OF 72 SKULL CAKE CASES)
1 (PACK OF 72 SKULL CAKE CASES) (12 PENCILS SMALL TUBE SKULL)
4 (36 DOILIES DOLLY GIRL) (ASSORTED BOTTLE TOP MAGNETS)
5 (ASSORTED BOTTLE TOP MAGNETS) (36 DOILIES DOLLY GIRL)
180 (CHILDRENS CUTLERY DOLLY GIRL) (CHILDRENS CUTLERY CIRCUS PARADE)
antecedent support consequent support support confidence lift \
0 0.055556 0.055556 0.055556 1.0 18.0
1 0.055556 0.055556 0.055556 1.0 18.0
4 0.055556 0.055556 0.055556 1.0 18.0
5 0.055556 0.055556 0.055556 1.0 18.0
180 0.055556 0.055556 0.055556 1.0 18.0
leverage conviction
0 0.052469 inf
1 0.052469 inf
4 0.052469 inf
5 0.052469 inf
180 0.052469 inf
On analyzing the above rules, it is found that boys’ and girls’ cutlery are paired together. This makes practical sense because when a parent goes shopping for cutlery for his/her children, he/she would want the product to be a little customized according to the kid’s wishes.