Which Machine Learning Algorithm is suitable for the case of Market Basket Analysis? Explain the steps involved.

Which Machine Learning Algorithm is suitable for the case of Market Basket Analysis? Explain the steps involved.

From the DPDC Assistant Manager (ICT) June 2025 – Written (BUET) exam, question 5 of 8.

Answers (1)

Market Basket Analysis finds out which products people tend to buy together. For example, "people who buy bread often also buy butter."

The most common algorithm for this is the Apriori algorithm. It belongs to a family called Association Rule Mining, which is a type of unsupervised learning. It is unsupervised because nobody tells the algorithm the answer in advance. It discovers the patterns by itself from shopping data.

Another algorithm, FP-Growth, does the same job faster on very large data, but Apriori is the standard one taught and is easier to understand.

Before the steps, you need three ideas. Suppose a shop has 5 customer receipts:

Receipt Items bought
1Bread, Milk
2Bread, Butter, Eggs
3Milk, Butter, Bread
4Bread, Butter
5Milk, Eggs

1. Support: How popular is an item or a group of items?

Support = (Number of receipts containing the items) ÷ (Total receipts)

Bread and Butter appear together in receipts 2, 3, and 4, so Support(Bread, Butter) = 3/5 = 60%.

2. Confidence: If someone buys A, how likely are they to also buy B?

Confidence(A → B) = Support(A and B) ÷ Support(A)

Bread appears in 4 receipts, and Butter is in 3 of those. So Confidence(Bread → Butter) = 3/4 = 75%. This means 75% of bread buyers also bought butter.

3. Lift: Is the link real, or just a coincidence?

Lift = Confidence(A → B) ÷ Support(B)

Butter appears in 3 out of 5 receipts, so Support(Butter) = 60%. Lift = 75% ÷ 60% = 1.25.

  • Lift > 1 means the items are truly bought together more than by chance.
  • Lift = 1 means there is no real connection.
  • Lift < 1 means buying one makes buying the other less likely.

Steps of the Apriori algorithm

Step 1: Collect the transaction data.

Gather the list of receipts showing what each customer bought.

Step 2: Set minimum thresholds.

Decide the minimum support and minimum confidence you care about. For example, min support = 50% and min confidence = 70%. Anything below these is ignored as too rare or too weak.

Step 3: Find frequent single items.

Count how often each item appears and keep only items that meet the minimum support.

  • Bread 4/5 = 80%, Milk 3/5 = 60%, Butter 3/5 = 60%
  • Eggs 2/5 = 40% (removed)

Step 4: Combine items into pairs, triples, and so on.

Make pairs only from the items that survived, then check their support again.

  • {Bread, Butter} = 60%
  • {Bread, Milk} = 40%
  • {Milk, Butter} = 20%

Keep growing the groups (pairs → triples → ...) until no new group passes the minimum support.

The key idea of Apriori: If an item is rare, any group containing it will also be rare. So once Eggs fails, the algorithm never wastes time checking groups with Eggs. This "pruning" is what makes Apriori efficient.

Step 5: Generate rules from the frequent groups.

From {Bread, Butter}, create rules such as:

  • Bread → Butter (confidence 75%)
  • Butter → Bread (confidence 3/3 = 100%)

Keep only the rules that meet the minimum confidence.

Step 6: Check the lift and select the useful rules.

Keep rules with lift greater than 1, because those show a real buying relationship.

Step 7: Use the results in business.

The shop can then act on the rules, for example by placing bread and butter close together, offering a combo discount, or showing "frequently bought together" suggestions online.

The Apriori algorithm finds products that are often bought together by first keeping only popular items, then building larger popular groups from them, and finally turning those groups into "if this, then that" rules measured by support, confidence, and lift.

— rhs

More from this paper