You are on page 1of 3

NAME- Gauransh verma

ROLL NO.- 2100290110054


DATA ANALYTICS LAB 11

To perform market basket analysis using Association Rules


(Apriori)

CODE:

Import libraries: from mlxtend.frequent_pa erns import apriori,


associa on_rules
Finding Frequent Itemsets: The first step is to find all frequent item sets in the
dataset. This is done by applying a minimum support threshold to iden fy item
sets that occur frequently enough in the dataset. In our code, using the apriori
func on from the mlxtend.frequent_pa erns module to achieve this. The
min_support parameter specifies the minimum support threshold.

frequent_itemsets = apriori(basket_sets, min_support=0.07,


use_colnames=True)

Genera ng Associa on Rules: Once frequent item sets have been iden fied,
the next step is to generate associa on rules based on these item sets. This is
done using the associa on_rules func on from the same module. We can
specify metrics such as confidence or li to evaluate the strength of the
associa on rules. In your code, using the li metric and specifying a
min_threshold of 1.
rules = associa on_rules(frequent_itemsets, metric="li ", min_threshold=1)

Displaying the Results: Finally, you can examine the generated associa on
rules. The head() func on is used to display the first few rows of the resul ng
DataFrame containing the associa on rules.
rules.head()
In this example:

1. We define a sample dataset transac ons, which represents a list of


transac ons where each transac on contains a set of items purchased.
2. We convert the transac onal data into a DataFrame df.
3. We convert the DataFrame into a one-hot encoded format using
pd.get_dummies() to prepare it for the Apriori algorithm.
4. We use the apriori func on to find frequent item sets with a minimum
support threshold of 0.4.
5. We use the associa on_rules func on to generate associa on rules with
a minimum li of 1.
6. Finally, we print the generated associa on rules.
Code:
# Impor ng necessary libraries

from mlxtend.frequent_pa erns import apriori, associa on_rules

import pandas as pd

# Sample transac onal data (replace this with your actual dataset)

transac ons = [

['bread', 'milk', 'vegetables'],

['bread', 'diapers', 'beer', 'eggs'],

['milk', 'diapers', 'beer', 'cola'],

['bread', 'milk', 'diapers', 'beer'],

['bread', 'milk', 'diapers', 'cola']

# Convert the transac onal data into a DataFrame

df = pd.DataFrame(transac ons, columns=['item1', 'item2', 'item3', 'item4'])

# Convert the DataFrame into a one-hot encoded format

basket_sets = pd.get_dummies(df)

# Find frequent item sets with minimum support of 0.4 (adjust as needed)

frequent_itemsets = apriori(basket_sets, min_support=0.4, use_colnames=True)


# Generate associa on rules with minimum li of 1

rules = associa on_rules(frequent_itemsets, metric="li ", min_threshold=1)

# Display the generated associa on rules

print(rules)

You might also like