0% found this document useful (0 votes)
9 views6 pages

Apriori Algorithm for Frequent Itemsets

The document describes analyzing a grocery store transaction dataset to identify frequent itemsets using the Apriori algorithm. It loads the dataset, preprocesses the data by grouping transactions by member and date, and encodes the transactions into a binary format. The Apriori algorithm is then applied with a minimum support level of 0.02 to identify itemsets that commonly occur together in transactions. The output shows the top 5 frequent itemsets by support level.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Apriori Algorithm for Frequent Itemsets

The document describes analyzing a grocery store transaction dataset to identify frequent itemsets using the Apriori algorithm. It loads the dataset, preprocesses the data by grouping transactions by member and date, and encodes the transactions into a binary format. The Apriori algorithm is then applied with a minimum support level of 0.02 to identify itemsets that commonly occur together in transactions. The output shows the top 5 frequent itemsets by support level.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2139472 Lab_3

December 1, 2021

1 Lab Program 3

2 Vinay Sirohi

3 2139472
3.1 Identify frequent item sets using Apriori Algorithm
[54]: #Importing mlxtend and printing the current version of it.
import mlxtend
print(mlxtend.__version__)

0.19.0

[2]: #Importing important libraries of our use


from mlxtend.frequent_patterns import apriori,association_rules
import numpy as np
import pandas as pd
from [Link] import TransactionEncoder
import time

[12]: #Reading out data set


df = pd.read_csv('Groceries_dataset.csv')
#Looking at the top 20 values of it.
[Link](20)

[12]: Member_number Date itemDescription


0 1808 21-07-2015 tropical fruit
1 2552 05-01-2015 whole milk
2 2300 19-09-2015 pip fruit
3 1187 12-12-2015 other vegetables
4 3037 01-02-2015 whole milk
5 4941 14-02-2015 rolls/buns
6 4501 08-05-2015 other vegetables
7 3803 23-12-2015 pot plants
8 2762 20-03-2015 whole milk
9 4119 12-02-2015 tropical fruit
10 1340 24-02-2015 citrus fruit

1
11 2193 14-04-2015 beef
12 1997 21-07-2015 frankfurter
13 4546 03-09-2015 chicken
14 4736 21-07-2015 butter
15 1959 30-03-2015 fruit/vegetable juice
16 1974 03-05-2015 packaged fruit/vegetables
17 2421 02-09-2015 chocolate
18 1513 03-08-2015 specialty bar
19 1905 07-07-2015 other vegetables

[24]: #Converting date column to proper format


df['Date'] = pd.to_datetime(df['Date'])
df

[24]: Member_number Date itemDescription


0 1808 2015-07-21 tropical fruit
1 2552 2015-05-01 whole milk
2 2300 2015-09-19 pip fruit
3 1187 2015-12-12 other vegetables
4 3037 2015-01-02 whole milk
… … … …
38760 4471 2014-08-10 sliced cheese
38761 2022 2014-02-23 candy
38762 1097 2014-04-16 cake bar
38763 1510 2014-03-12 fruit/vegetable juice
38764 1521 2014-12-26 cat food

[38765 rows x 3 columns]

[27]: #Grouping the transactions by member number and date


df['itemDescription'] = [Link](['Member_number',␣
,→'Date'])['itemDescription'].transform(lambda x: ','.join(x))

df

[27]: Member_number Date \


0 1808 2015-07-21
1 2552 2015-05-01
2 2300 2015-09-19
3 1187 2015-12-12
4 3037 2015-01-02
… … …
38760 4471 2014-08-10
38761 2022 2014-02-23
38762 1097 2014-04-16
38763 1510 2014-03-12
38764 1521 2014-12-26

2
itemDescription
0 tropical fruit,rolls/buns,candy,tropical fruit…
1 whole milk,tropical fruit,chocolate,whole milk…
2 pip fruit,other vegetables,flour,pip fruit,oth…
3 other vegetables,onions,shopping bags,other ve…
4 whole milk,other vegetables,white bread,whole …
… …
38760 whole milk,yogurt,sliced cheese,whole milk,yog…
38761 cat food,yogurt,candy,cat food,yogurt,candy,ca…
38762 sausage,whole milk,cake bar,sausage,whole milk…
38763 beef,canned beer,fruit/vegetable juice,beef,ca…
38764 ham,seasonal products,cat food,ham,seasonal pr…

[38765 rows x 3 columns]

[34]: #Let's now create a list of the transactions so that we can transform our data␣
,→into the correct format using TransactionEncoder.

df1=[]
for i in range(0,len(df)-1):
data = df['itemDescription'][i].split(',')
[Link](data)

[59]: #Applying transaction encoder to our data set


te = TransactionEncoder()
te_ary = [Link](df1).transform(df1)
df2 = [Link](te_ary, columns=te.columns_)
df2[:10]

[59]: Instant food products UHT-milk abrasive cleaner artif. sweetener \


0 False False False False
1 False False False False
2 False False False False
3 False False False False
4 False False False False
5 False False False False
6 False False False False
7 False False False False
8 False False False False
9 False False False False

baby cosmetics bags baking powder bathroom cleaner beef berries \


0 False False False False False False
1 False False False False False False
2 False False False False False False
3 False False False False False False
4 False False False False False False
5 False False False False False False

3
6 False False False False False False
7 False False False False False False
8 False False False False False False
9 False False False False False False

… turkey vinegar waffles whipped/sour cream whisky white bread \


0 … False False False False False False
1 … False False False False False False
2 … False False False False False False
3 … False False False False False False
4 … False False False False False True
5 … False False False False False False
6 … False False False False False False
7 … False False False True False False
8 … False False False False False False
9 … False False False False False False

white wine whole milk yogurt zwieback


0 False False False False
1 False True False False
2 False False False False
3 False False False False
4 False True False False
5 False False False False
6 False False False False
7 False False False False
8 False True False False
9 False False False False

[10 rows x 167 columns]

[62]: #Applying apriori to look at itemsets which have a minimum support level of 0.02
frq = apriori(df2, min_support=0.02)
[Link]()

[62]: support itemsets


0 0.024533 (1)
1 0.039624 (8)
2 0.024739 (9)
3 0.054742 (11)
4 0.069497 (12)

A float between 0 and 1 for minimum support of the itemsets returned. The support is computed
as the fraction transactions_where_item(s)_occur / total_transactions.

[63]: #Use colnames = True to look at the itemsets by name


fr = apriori(df2, min_support=0.02, use_colnames=True)
[Link]()

4
[63]: support itemsets
0 0.024533 (UHT-milk)
1 0.039624 (beef)
2 0.024739 (berries)
3 0.054742 (bottled beer)
4 0.069497 (bottled water)

[42]: #applying lambda function to look at the length of the itemsets


frequent_itemsets = apriori(df2, min_support=0.02, use_colnames=True)
frequent_itemsets['length'] = frequent_itemsets['itemsets'].apply(lambda x:␣
,→len(x))

frequent_itemsets

[42]: support itemsets length


0 0.024533 (UHT-milk) 1
1 0.039624 (beef) 1
2 0.024739 (berries) 1
3 0.054742 (bottled beer) 1
4 0.069497 (bottled water) 1
5 0.044165 (brown bread) 1
6 0.041895 (butter) 1
7 0.020586 (butter milk) 1
8 0.055954 (canned beer) 1
9 0.033072 (chicken) 1
10 0.028093 (chocolate) 1
11 0.060649 (citrus fruit) 1
12 0.036658 (coffee) 1
13 0.027035 (cream cheese ) 1
14 0.040914 (curd) 1
15 0.026029 (dessert) 1
16 0.044139 (domestic eggs) 1
17 0.047983 (frankfurter) 1
18 0.020638 (frozen meals) 1
19 0.033201 (frozen vegetables) 1
20 0.040140 (fruit/vegetable juice) 1
21 0.025384 (hamburger meat) 1
22 0.021128 (long life bakery product) 1
23 0.038464 (margarine) 1
24 0.026287 (napkins) 1
25 0.046048 (newspapers) 1
26 0.023140 (onions) 1
27 0.137679 (other vegetables) 1
28 0.060623 (pastry) 1
29 0.057708 (pip fruit) 1
30 0.043778 (pork) 1
31 0.127954 (rolls/buns) 1
32 0.079404 (root vegetables) 1

5
33 0.022289 (salty snack) 1
34 0.074580 (sausage) 1
35 0.056444 (shopping bags) 1
36 0.112269 (soda) 1
37 0.021128 (sugar) 1
38 0.079326 (tropical fruit) 1
39 0.022315 (waffles) 1
40 0.048344 (whipped/sour cream) 1
41 0.028841 (white bread) 1
42 0.183753 (whole milk) 1
43 0.101434 (yogurt) 1
44 0.022340 (other vegetables, whole milk) 2
45 0.022237 (whole milk, rolls/buns) 2

[48]: #selecting itemsets which have a minimum support level of 0.02 and lenght 2
frequent_itemsets[ (frequent_itemsets['length'] == 2) &
(frequent_itemsets['support'] > 0.02) ]

[48]: support itemsets length


44 0.022340 (other vegetables, whole milk) 2
45 0.022237 (whole milk, rolls/buns) 2

[61]: #Applying association rules with minimum threshold value 0.02


rules = association_rules(frequent_itemsets , metric="confidence",␣
,→min_threshold=0.02)

rules

[61]: antecedents consequents antecedent support \


0 (other vegetables) (whole milk) 0.137679
1 (whole milk) (other vegetables) 0.183753
2 (whole milk) (rolls/buns) 0.183753
3 (rolls/buns) (whole milk) 0.127954

consequent support support confidence lift leverage conviction


0 0.183753 0.022340 0.162263 0.883052 -0.002959 0.974348
1 0.137679 0.022340 0.121578 0.883052 -0.002959 0.981670
2 0.127954 0.022237 0.121016 0.945782 -0.001275 0.992108
3 0.183753 0.022237 0.173790 0.945782 -0.001275 0.987942

You might also like