Apriori 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
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 \
45 (JUMBO BAG WOODLAND ANIMALS)
258 (RED TOADSTOOL LED NIGHT LIGHT, PLASTERS IN TI...
272 (RED TOADSTOOL LED NIGHT LIGHT, PLASTERS IN TI...
300 (SET/6 RED SPOTTY PAPER CUPS, SET/20 RED RETRO...
302 (SET/20 RED RETROSPOT PAPER NAPKINS, SET/6 RED...
consequents antecedent support consequent support \
45 (POSTAGE) 0.076531 0.765306
258 (POSTAGE) 0.051020 0.765306
272 (POSTAGE) 0.053571 0.765306
300 (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
45 0.076531 1.000 1.306667 0.017961 inf
258 0.051020 1.000 1.306667 0.011974 inf
272 0.053571 1.000 1.306667 0.012573 inf
300 0.099490 0.975 7.644000 0.086474 34.897959
302 0.099490 0.975 7.077778 0.085433 34.489796
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 12 COLOUR PENCILS DOLLY GIRL) (SET OF 4 KNICK KNACK TINS POPPIES)
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 CIRCUS PARADE) (CHILDRENS CUTLERY DOLLY GIRL)
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.