VOOZH about

URL: https://www.geeksforgeeks.org/machine-learning/implementing-apriori-algorithm-in-python/

⇱ Implementing Apriori algorithm in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Implementing Apriori algorithm in Python

Last Updated : 2 May, 2026

Apriori Algorithm is a frequent itemset mining algorithm used for market basket analysis. It helps to find associations or relationships between items in large transactional datasets. A common real-world application is product recommendation where items are suggested to users based on their shopping cart contents.

Step 1: Importing Required Libraries

Before we begin we need to import the necessary Python libraries like Pandas , Numpy and mlxtend.

Step 2: Loading and exploring the data

We start by loading a popular groceries dataset. This dataset contains customer transactions with details like customer ID, transaction date, and the item purchased. you can download the dataset from here.

Output:

πŸ‘ Dataset
Dataset
  • Each row represents one item in a customer's basket on a given date.
  • To use the Apriori algorithm we must convert this into full transactions per customer per visit.

Step 3: Group Items by Transaction

We group items purchased together by the same customer on the same day to form one transaction.

Output:

πŸ‘ Group-data-items
Group items

Step 4: Convert to One-Hot Format

Apriori needs data in True/False format like Did the item appear in the basket?. We use Transaction Encoder for this:

Step 5: Run Apriori Algorithm

Now we find frequent itemsets combinations of items that often occur together. Here min_support=0.01 means itemsets that appear in at least 1% of transactions. This gives us common combinations of items.

Output:

Total Frequent Itemsets: 69

Step 6: Generate Association Rules

Now we find rules like If bread and butter are bought, milk is also likely to be bought.

  • Support: How often the rule appears in the dataset.
  • Confidence: Probability of buying item B if item A is bought.
  • Lift: Strength of the rule over random chance. (>1 means it's a good rule)

Output:

πŸ‘ Generate-Association-rules
Association rules

Step 7: Visualize the Most Popular Items

Let’s see which items are most frequently bought:

Output:

πŸ‘ download-
Most Purchased Items

As shown in the above output Whole milk is the most frequently bought item, followed by other vegetables, rolls/bun and soda.

Comment
Article Tags: