Sie sind auf Seite 1von 4

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)

Arm Band Bluetooth Headset Leather Pouch Mobile Cover Power Bank \
0 False False False False True
1 False True False True False
2 True False False True False
3 False False True False True
4 False True False True True

Screen Guard Travel Charger


0 True True
1 True False
2 True False
3 True False
4 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)

antecedents consequents \
0 (Mobile Cover, Power Bank) (Bluetooth Headset)
1 (Power Bank, Bluetooth Headset) (Mobile Cover)
2 (Mobile Cover, Arm Band) (Screen Guard)
3 (Screen Guard, Arm Band) (Mobile Cover)
4 (Arm Band) (Mobile Cover, Screen Guard)
5 (Travel Charger) (Screen Guard)
6 (Travel Charger) (Power Bank)
7 (Bluetooth Headset) (Mobile Cover)
8 (Leather Pouch) (Power Bank)
9 (Screen Guard, Travel Charger) (Power Bank)
10 (Power Bank, Travel Charger) (Screen Guard)
11 (Travel Charger) (Screen Guard, Power Bank)
12 (Leather Pouch, Power Bank) (Screen Guard)
13 (Leather Pouch, Screen Guard) (Power Bank)
14 (Leather Pouch) (Screen Guard, Power Bank)
15 (Arm Band) (Screen Guard)
16 (Leather Pouch) (Screen Guard)
17 (Arm Band) (Mobile Cover)
18 (Screen Guard, Bluetooth Headset) (Mobile Cover)

antecedent support consequent support support confidence lift \


0 0.2 0.4 0.2 1.0 2.500000
1 0.2 0.6 0.2 1.0 1.666667
2 0.2 0.8 0.2 1.0 1.250000
3 0.2 0.6 0.2 1.0 1.666667
4 0.2 0.4 0.2 1.0 2.500000
5 0.2 0.8 0.2 1.0 1.250000
6 0.2 0.6 0.2 1.0 1.666667
7 0.4 0.6 0.4 1.0 1.666667
8 0.2 0.6 0.2 1.0 1.666667
9 0.2 0.6 0.2 1.0 1.666667
10 0.2 0.8 0.2 1.0 1.250000
11 0.2 0.4 0.2 1.0 2.500000
12 0.2 0.8 0.2 1.0 1.250000
13 0.2 0.6 0.2 1.0 1.666667
14 0.2 0.4 0.2 1.0 2.500000
15 0.2 0.8 0.2 1.0 1.250000
16 0.2 0.8 0.2 1.0 1.250000
17 0.2 0.6 0.2 1.0 1.666667
18 0.2 0.6 0.2 1.0 1.666667

leverage conviction
0 0.12 inf
1 0.08 inf
2 0.04 inf
3 0.08 inf
4 0.12 inf
5 0.04 inf
6 0.08 inf
7 0.16 inf
8 0.08 inf
9 0.08 inf
https://2886795294-8888-host04-fresco.environments.katacoda.com/notebooks/ASM-hands_on.ipynb 3/4
11/9/2018 ASM-hands_on

10 0.04 inf
11 0.12 inf
12 0.04 inf
13 0.08 inf
14 0.12 inf
15 0.04 inf
16 0.04 inf
17 0.08 inf
18 0.08 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

Das könnte Ihnen auch gefallen