ASSIGNMENT 6
In [7]: #Iprori algorithms
# Import Libraries
import pandas as pd
import numpy as np
In [10]: #Load the dataset
dataset =
['Milk', 'Onion", 'Nutmeg', 'Kidney Beans", 'Eggs", 'Yogurt'],
['Dill', 'Onion", 'Nutmeg", 'Kidney Beans", 'Eggs", 'Yogurt'],
['Milk", 'Apple", 'Kidney Beans", 'Eggs'],
['Milk", 'Unicorn", 'Corn", 'Kidney Beans", 'Yogurt'],
['Corn', 'Onion', 'Onion', 'Kidney Beans', 'Ice cream', 'Eggs']
#!pip install mlxtend
In [9]: # Importing the apriori function and transaction encoder
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori
# Assuming your dataset is a List of Lists where each inner List is a transaction
# If not, you might need to preprocess your data to get it in this format
# Applying the transaction encoder
te = TransactionEncoder()
te_ary = te.fit(dataset).transform(dataset)
df = pd.DataFrame(te_ary, columns=te.columns_)
# Applying the apriori algorithm
frequent_itemsets = apriori(df, min_support=0.6, use_colnames=True)
print(frequent_itemsets)
support itemsets
0 0.8 (Eggs)
1 1.0 (Kidney Beans)
2 0.6 (Milk)
3 0.6 (Onion)
4 0.6 (Yogurt)
5 0.8 (Eggs, Kidney Beans)
6 0.6 (Eggs, Onion)
7 0.6 (Milk, Kidney Beans)
8 0.6 (Kidney Beans, Onion)
9 0.6 (Yogurt, Kidney Beans)
10 0.6 (Eggs, Kidney Beans, Onion)
In [ ] :
1/1