Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#

Apriori Algorithm

Rate me:
Please Sign up or sign in to vote.
4.91/5 (84 votes)
10 Aug 2012CPOL2 min read 577.7K   10.8K   149   149
Implementation of the Apriori algorithm in C#.

Image 1

Introduction

In data mining, Apriori is a classic algorithm for learning association rules. Apriori is designed to operate on databases containing transactions (for example, collections of items bought by customers, or details of a website frequentation).

Other algorithms are designed for finding association rules in data having no transactions (Winepi and Minepi), or having no timestamps (DNA sequencing).

Overview

The whole point of the algorithm (and data mining, in general) is to extract useful information from large amounts of data. For example, the information that a customer who purchases a keyboard also tends to buy a mouse at the same time is acquired from the association rule below:

Support: The percentage of task-relevant data transactions for which the pattern is true.

Support (Keyboard -> Mouse) = AprioriAlgorithm/eq_1.JPG

Confidence: The measure of certainty or trustworthiness associated with each discovered pattern.

Confidence (Keyboard -> Mouse) = AprioriAlgorithm/eq_2.JPG

The algorithm aims to find the rules which satisfy both a minimum support threshold and a minimum confidence threshold (Strong Rules).

  • Item: article in the basket.
  • Itemset: a group of items purchased together in a single transaction.

How Apriori Works

  1. Find all frequent itemsets:
    • Get frequent items:
      • Items whose occurrence in database is greater than or equal to the min.support threshold.
    • Get frequent itemsets:
      • Generate candidates from frequent items.
      • Prune the results to find the frequent itemsets.
  2. Generate strong association rules from frequent itemsets
    • Rules which satisfy the min.support and min.confidence threshold.

High Level Design

AprioriAlgorithm/1-Apriori_HLD_Big.JPG

Low Level Design

AprioriAlgorithm/Apriori_LLD.jpg 

Example 

A database has five transactions. Let the min sup = 50% and min con f = 80%.

AprioriAlgorithm/3-DB.JPG

Solution 

Step 1: Find all Frequent Itemsets

AprioriAlgorithm/4-Example.JPG

Frequent Itemsets

{A}   {B}   {C}   {E}   {A C}   {B C}   {B E}   {C E}   {B C E}

Step 2: Generate strong association rules from the frequent itemsets

AprioriAlgorithm/5-StrongRules.JPG

Lattice

Closed Itemset: support of all parents are not equal to the support of the itemset.

Maximal Itemset: all parents of that itemset must be infrequent.

Keep in mind:

AprioriAlgorithm/6-Set.JPG

AprioriAlgorithm/7-Lattice.JPG

Itemset {c} is closed as support of parents (supersets) {A C}:2, {B C}:2, {C D}:1, {C E}:2 not equal support of {c}:3.

And the same for {A C}, {B E} & {B C E}.

Itemset {A C} is maximal as all parents (supersets) {A B C}, {A C D}, {A C E} are infrequent.

And the same for {B C E}.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Egypt Egypt
Enthusiastic programmer/researcher, passionate to learn new technologies, interested in problem solving, data structures, algorithms, AI, machine learning and nlp.

Amateur guitarist/ keyboardist, squash player.

Comments and Discussions

 
Questionerror i got.. Pin
Member 79873118-Oct-11 8:06
Member 79873118-Oct-11 8:06 
AnswerRe: error i got.. Pin
Omar Gameel Salem8-Oct-11 8:15
professionalOmar Gameel Salem8-Oct-11 8:15 
GeneralRe: error i got.. Pin
Member 79873118-Oct-11 23:43
Member 79873118-Oct-11 23:43 
GeneralRe: error i got.. Pin
Omar Gameel Salem10-Oct-11 23:42
professionalOmar Gameel Salem10-Oct-11 23:42 
Questionapriori algorithm Pin
salv0327-Sep-11 7:03
salv0327-Sep-11 7:03 
GeneralMy vote of 5 Pin
Milad Rk6-Sep-11 22:19
Milad Rk6-Sep-11 22:19 
QuestionHelp on modifications Pin
Member 811009825-Jul-11 0:36
Member 811009825-Jul-11 0:36 
AnswerRe: Help on modifications Pin
Omar Gameel Salem25-Jul-11 1:46
professionalOmar Gameel Salem25-Jul-11 1:46 
Thank you Smile | :)
well , why don't you just reverse the condition in AddStrongRule function ?
like this :

private void AddStrongRule(clssRules Rule, string strXY, ref List<clssRules> lstStrongRulesReturn, double dMinConfidence)
{
    double dConfidence = GetConfidence(Rule.X, strXY);
    clssRules NewRule;
    if (dConfidence < dMinConfidence)
    {
        NewRule = new clssRules(Rule.X, Rule.Y, dConfidence);
        lstStrongRulesReturn.Add(NewRule);
    }
    dConfidence = GetConfidence(Rule.Y, strXY);
    if (dConfidence < dMinConfidence)
    {
        NewRule = new clssRules(Rule.Y, Rule.X, dConfidence);
        lstStrongRulesReturn.Add(NewRule);
    }
}

GeneralRe: Help on modifications Pin
Member 811009825-Jul-11 3:39
Member 811009825-Jul-11 3:39 
GeneralRe: Help on modifications Pin
Omar Gameel Salem25-Jul-11 23:17
professionalOmar Gameel Salem25-Jul-11 23:17 
GeneralRe: Help on modifications Pin
Member 811009825-Jul-11 23:31
Member 811009825-Jul-11 23:31 
Questionrevision of borderset Pin
Member 40828605-Jul-11 18:28
Member 40828605-Jul-11 18:28 
QuestionInput help in C++ for prices' algorithm(similar to apriori) Pin
adiiscool327-May-11 22:54
adiiscool327-May-11 22:54 
AnswerRe: Input help in C++ for prices' algorithm(similar to apriori) Pin
salv0327-Sep-11 7:05
salv0327-Sep-11 7:05 
GeneralMy vote of 5 Pin
Filip D'haene24-May-11 7:09
Filip D'haene24-May-11 7:09 
Generalhello Pin
salsaaabail12-May-11 23:19
salsaaabail12-May-11 23:19 
GeneralRe: hello Pin
Omar Gameel Salem13-May-11 0:09
professionalOmar Gameel Salem13-May-11 0:09 
GeneralRe: hello Pin
Member 421924531-Oct-11 11:27
Member 421924531-Oct-11 11:27 
GeneralMy vote of 5 Pin
Philip.F11-May-11 3:07
Philip.F11-May-11 3:07 
GeneralMy vote of 5 Pin
somil19872-May-11 21:20
somil19872-May-11 21:20 
GeneralThanks Pin
soso.eng27-Apr-11 7:00
soso.eng27-Apr-11 7:00 
Generalthanx Pin
Member 778383824-Mar-11 18:07
Member 778383824-Mar-11 18:07 
Generalhelp needed Pin
Antony Rose Welt21-Mar-11 4:43
Antony Rose Welt21-Mar-11 4:43 
GeneralMy vote of 5 Pin
gayatrisri12-Mar-11 6:51
gayatrisri12-Mar-11 6:51 
GeneralRe: My vote of 5 Pin
Tarun.K.S12-Mar-11 7:37
Tarun.K.S12-Mar-11 7:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.