Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

NAME:-Mayur Pawade

Batch :- C3
Roll No:- 219
TE AIML

Exp 9:- APRIORI ALGORITHM USING PYTHON/JAVA


Aim:- To implement Apriori Algorithm using Python/Java.

Theory:-
Apriori Algorithm
Apriori algorithm refers to the algorithm which is used to calculate the
association rules between objects. It means how two or more objects are related
to one another. In other words, we can say that the apriori algorithm is an
association rule leaning that analyzes that people who bought product A also
bought product B.
The primary objective of the apriori algorithm is to create the association rule
between different objects. The association rule describes how two or more
objects are related to one another. Apriori algorithm is also called frequent
pattern mining. Generally, you operate the Apriori algorithm on a database that
consists of a huge number of transactions. Let's understand the apriori algorithm
with the help of an example; suppose you go to Big Bazar and buy different
products. It helps the customers buy their products with ease and increases the
sales performance of the Big Bazar. In this tutorial, we will discuss the apriori
algorithm with examples.
Introduction
We take an example to understand the concept better. You must have noticed
that the Pizza shop seller makes a pizza, soft drink, and breadstick combo
together. He also offers a discount to their customers who buy these combos. Do
you ever think why does he do so? He thinks that customers who buy pizza also
buy soft drinks and breadsticks. However, by making combos, he makes it easy
for the customers. At the same time, he also increases his sales performance.
Similarly, you go to Big Bazar, and you will find biscuits, chips, and Chocolate
bundled together. It shows that the shopkeeper makes it comfortable for the
customers to buy these products in the same place.
The above two examples are the best examples of Association Rules in Data
Mining
. It helps us to learn the concept of apriori algorithms.
NAME:-Mayur Pawade
Batch :- C3
Roll No:- 219
TE AIML
What is Apriori Algorithm?
Apriori algorithm refers to an algorithm that is used in mining frequent products
sets and relevant association rules. Generally, the apriori algorithm operates on a
database containing a huge number of transactions. For example, the items
customers but at a Big Bazar.
Apriori algorithm helps the customers to buy their products with ease and
increases the sales performance of the particular store.
Components of Apriori algorithm
The given three components comprise the apriori algorithm.
1. Support
2. Confidence
3. Lift

Let's take an example to understand this concept.


We have already discussed above; you need a huge database containing a large
no of transactions. Suppose you have 4000 customers transactions in a Big
Bazar. You have to calculate the Support, Confidence, and Lift for two products,
and you may say Biscuits and Chocolate. This is because customers frequently
buy these two items together.
Out of 4000 transactions, 400 contain Biscuits, whereas 600 contain Chocolate,
and these 600 transactions include a 200 that includes Biscuits and chocolates.
Using this data, we will find out the support, confidence, and lift.
Advantages of Apriori Algorithm
o It is used to calculate large itemsets.
o Simple to understand and apply.

Disadvantages of Apriori Algorithms


o Apriori algorithm is an expensive method to find support since the
calculation has to pass through the whole database.
o Sometimes, you need a huge number of candidate rules, so it becomes
computationally more expensive.
NAME:-Mayur Pawade
Batch :- C3
Roll No:- 219
TE AIML
CODE:-
def select_candidates(frequent_is, support):

candidate_items = []

for key in frequent_is:

if frequent_is[key] >= support:

candidate_items.append(key)

return candidate_items

min_support = 6

min_confidence = 80

iterations = 1

file = open('Test.csv', 'r')

transactions = {}

for line in file:

transact = line.strip().split(',')

transactions[transact[0]] = transact[1:]

item_support = {}

for key in transactions:


NAME:-Mayur Pawade
Batch :- C3
Roll No:- 219
TE AIML
for value in transactions[key]:

if value not in item_support:

item_support[value] = 1

else:

item_support[value] += 1

print('\nCandidate Itemset for iteration ' + str(iterations))

print('\nItems - Support')

for key in item_support:

print(key + ' -\t' + str(item_support[key]))

candidates = select_candidates(item_support, min_support)

print('\nFrequent Itemset for iteration ' + str(iterations))

print('\nItems - Support')

for vals in candidates:

print(vals + ' -\t' + str(item_support[vals]))

iterations += 1

frequent_items = {}

for i in range(len(candidates) - 1):

for j in range(i + 1, len(candidates)):


NAME:-Mayur Pawade
Batch :- C3
Roll No:- 219
TE AIML
key = tuple(sorted((candidates[i], candidates[j])))

if key not in frequent_items:

frequent_items[key] = 0

for keys in frequent_items:

el1, el2 = keys

for element in transactions:

if el1 in transactions[element] and el2 in transactions[element]:

frequent_items[keys] += 1

print('***********************************')

print('\nCandidate Itemset for iteration ' + str(iterations))

print('\nItems - Support')

for key in frequent_items:

print(str(key) + ' - ' + str(frequent_items[key]))

item_support_iter2 = frequent_items

candidates = select_candidates(frequent_items, min_support)

print('\nFrequent Itemset for iteration ' + str(iterations))

print('\nItems - Support')
NAME:-Mayur Pawade
Batch :- C3
Roll No:- 219
TE AIML
for vals in candidates:

print(str(vals) + ' - ' + str(frequent_items[vals]))

iterations += 1

frequent_items = {}

for i in range(len(candidates) - 1):

for j in range(i + 1, len(candidates)):

el1, el2 = candidates[i]

if el1 in candidates[j] or el2 in candidates[j]:

item = tuple(sorted(set([el1, el2, candidates[j][0], candidates[j][1]])))

if item not in frequent_items:

frequent_items[item] = 0

for keys in frequent_items:

el1, el2, el3 = keys

for element in transactions:

if el1 in transactions[element] and el2 in transactions[

element] and el3 in transactions[element]:

frequent_items[keys] += 1

print('***********************************')
NAME:-Mayur Pawade
Batch :- C3
Roll No:- 219
TE AIML
print('\nCandidate Itemset for iteration ' + str(iterations))

print('\nItems - Support')

for key in frequent_items:

print(str(key) + ' - ' + str(frequent_items[key]))

candidates = select_candidates(frequent_items, min_support)

print('\nFrequent Itemset for iteration ' + str(iterations))

print('\nItems - Support')

for vals in candidates:

print(str(vals) + ' - ' + str(frequent_items[vals]))

iterations += 1

Association_rules = {}

print('\nAssociation rules with confidence')

for i in range(3):

association = candidates[0][i] + ' -> ' + candidates[0][

(i + 1) % 3] + ' ^ ' + candidates[0][(i + 2) % 3]

confidence = frequent_items[candidates[0]] * 100 /


item_support[candidates[0]
NAME:-Mayur Pawade
Batch :- C3
Roll No:- 219
TE AIML
[i]]

Association_rules[association] = confidence

print(association + '\tConfidence = ' + str(confidence))

for i in range(3):

key = tuple(sorted((candidates[0][i], candidates[0][(i + 1) % 3])))

association = candidates[0][i] + ' ^ ' + candidates[0][

(i + 1) % 3] + ' -> ' + candidates[0][(i + 2) % 3]

confidence = frequent_items[candidates[0]] * 100 / item_support_iter2[key]

Association_rules[association] = confidence

print(association + '\tConfidence = ' + str(confidence))

print('\nStrong association rules with confidence greater than or equal 80%')

for key in Association_rules:

if Association_rules[key] >= min_confidence:

print(key + '\tConfidence = ' + str(Association_rules[key]))

OUTPUT:-
NAME:-Mayur Pawade
Batch :- C3
Roll No:- 219
TE AIML

dataset:
T1,A,B,C,D
T2,A,B,C,D
T3,A,B,C
T4,A,B
T5,A,B,C
T6,A,C,D
T7,B,C,D
T8,B,C,D
T9,A,B,C
T10,A,B,C
NAME:-Mayur Pawade
Batch :- C3
Roll No:- 219
TE AIML
Conclusion:- Thus we have learned how to implement Apriori
Algorithm using Python/Java.

You might also like