Association Rule Mining-hands_on

11/9/2018 ASM-hands_on Welcome to the first Hands On association rule mining. In this exercise , you will try out ASM

Views 448 Downloads 29 File size 143KB

Report DMCA / Copyright

DOWNLOAD FILE

Recommend stories

Citation preview

11/9/2018

ASM-hands_on

Welcome to the first Hands On association rule mining. In this exercise , you will try out ASM regression using mlxtend library that you have learnt in the course. We have created this Python Notebook with all the necessary things needed for completing this exercise. You have to write your code in between the are mentioned

Start Code



Your Code here

End Code To run the code in each cell click on the cell and press shift + enter Run the below cell to load the data on which you will be performing ASM. The data has the records of items purcahed where each element of 'Data' refers to a single transaction In [1]: Data = [['Power Bank', 'Screen Guard' , 'Travel Charger'], ['Screen Guard', 'Bluetooth Headset', 'Mobile Cover'], ['Screen Guard','Arm Band','Mobile Cover'], ['Power Bank','Screen Guard','Leather Pouch'], ['Bluetooth Headset', 'Power Bank' , 'Mobile Cover']] Run the below cell to import necessary packages to perform AMS In [2]: import pandas as pd from mlxtend.preprocessing import OnehotTransactions from mlxtend.frequent_patterns import apriori - initalize OnehotTransactions as oht - fit and transform the transaction data('Data') to perform one hot encoding - create a dataframe of one hot encoded data - Find all frequent item sets with minimum support 0.1 using apriori function **Follow the code snippet in the course on implementing the above steps**

https://2886795294-8888-host04-fresco.environments.katacoda.com/notebooks/ASM-hands_on.ipynb

1/4

11/9/2018

ASM-hands_on

In [13]: ###Start code here oht = OnehotTransactions() oht_ary = oht.fit(Data).transform(Data) dataFrame = pd.DataFrame(oht_ary, columns=oht.columns_) frequent_itemsets = apriori(dataFrame, min_support=0.1, use_colnames=True) print(dataFrame) ###End code(approx 4 lines) 0 1 2 3 4

Arm Band False False True False False

Bluetooth Headset False True False False True

0 1 2 3 4

Screen Guard True True True True False

Leather Pouch False False False True False

Mobile Cover False True True False True

Power Bank True False False True True

\

Travel Charger True False False False False

/home/scrapbook/.local/lib/python3.5/site-packages/mlxtend/preprocessing/one hot.py:66: DeprecationWarning: OnehotTransactions has been deprecated and wi ll be removed in future. Please use TransactionEncoder instead. warnings.warn(msg, DeprecationWarning) generate association rule for all the itemsets(frequent_itemsets) with minimum confidence 0.7

https://2886795294-8888-host04-fresco.environments.katacoda.com/notebooks/ASM-hands_on.ipynb

2/4

11/9/2018

ASM-hands_on

In [14]: from mlxtend.frequent_patterns import association_rules ###Start code here association_rule = association_rules(frequent_itemsets, metric="confidence", min_threshold= print(association_rule) ###End code(approx 2 lines) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

antecedents (Mobile Cover, Power Bank) (Power Bank, Bluetooth Headset) (Mobile Cover, Arm Band) (Screen Guard, Arm Band) (Arm Band) (Travel Charger) (Travel Charger) (Bluetooth Headset) (Leather Pouch) (Screen Guard, Travel Charger) (Power Bank, Travel Charger) (Travel Charger) (Leather Pouch, Power Bank) (Leather Pouch, Screen Guard) (Leather Pouch) (Arm Band) (Leather Pouch) (Arm Band) (Screen Guard, Bluetooth Headset)

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

antecedent support 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.4 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2

0 1 2 3 4 5 6 7 8 9

leverage 0.12 0.08 0.04 0.08 0.12 0.04 0.08 0.16 0.08 0.08

consequents (Bluetooth Headset) (Mobile Cover) (Screen Guard) (Mobile Cover) (Mobile Cover, Screen Guard) (Screen Guard) (Power Bank) (Mobile Cover) (Power Bank) (Power Bank) (Screen Guard) (Screen Guard, Power Bank) (Screen Guard) (Power Bank) (Screen Guard, Power Bank) (Screen Guard) (Screen Guard) (Mobile Cover) (Mobile Cover)

consequent support 0.4 0.6 0.8 0.6 0.4 0.8 0.6 0.6 0.6 0.6 0.8 0.4 0.8 0.6 0.4 0.8 0.8 0.6 0.6

support 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.4 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2

confidence 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0

\

lift 2.500000 1.666667 1.250000 1.666667 2.500000 1.250000 1.666667 1.666667 1.666667 1.666667 1.250000 2.500000 1.250000 1.666667 2.500000 1.250000 1.250000 1.666667 1.666667

\

conviction inf inf inf inf inf inf inf inf inf inf

https://2886795294-8888-host04-fresco.environments.katacoda.com/notebooks/ASM-hands_on.ipynb

3/4

11/9/2018

10 11 12 13 14 15 16 17 18

ASM-hands_on

0.04 0.12 0.04 0.08 0.12 0.04 0.04 0.08 0.08

inf inf inf inf inf inf inf inf inf

What is the consequent support value for Leather Pouch -> Screen Guard ? What is the lift value for (Arm Band, Mobile Cover)->(Screen Guard) ? In how many scenarios do you see 2 items (dualtons) in the antecedent set ? assign the above abservations to respective variable in the cell below

In [15]: ###Start code here support = 0.8 lift = 1.250000 dualtons = 9 ###End code(approx 3 lines) with open("output.txt", "w") as text_file: text_file.write("support= %f\n" % support) text_file.write("lift= %f\n" % lift) text_file.write("dualtons= %f\n" % dualtons) In [ ]:

https://2886795294-8888-host04-fresco.environments.katacoda.com/notebooks/ASM-hands_on.ipynb

4/4