0% found this document useful (0 votes)
39 views1 page

Python

This document discusses applying the apriori algorithm from the mlxtend library in Python to find frequent itemsets in a sample transactional dataset. The dataset is loaded and preprocessed by applying a transaction encoder. The encoded data is then passed to the apriori function, which returns the frequent itemsets that occur in at least 60% of transactions. The results show the most common individual items and combinations of items purchased together.

Uploaded by

2147033
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)
39 views1 page

Python

This document discusses applying the apriori algorithm from the mlxtend library in Python to find frequent itemsets in a sample transactional dataset. The dataset is loaded and preprocessed by applying a transaction encoder. The encoded data is then passed to the apriori function, which returns the frequent itemsets that occur in at least 60% of transactions. The results show the most common individual items and combinations of items purchased together.

Uploaded by

2147033
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
You are on page 1/ 1

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

You might also like