0% found this document useful (0 votes)
109 views

Apriori Algorithm - Ipynb - Colaboratory

The document describes the apriori algorithm for finding frequent itemsets in a dataset. It shows the algorithm being applied to sample transaction data containing customers and items. The algorithm finds singleton itemsets that meet a minimum support threshold, then generates and tests candidate itemsets of increasing size to find those that are frequent. The final frequent itemsets found are ['I1', 'I2', 'I3'] and ['I2', 'I5', 'I1'].

Uploaded by

Dreaming Boy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views

Apriori Algorithm - Ipynb - Colaboratory

The document describes the apriori algorithm for finding frequent itemsets in a dataset. It shows the algorithm being applied to sample transaction data containing customers and items. The algorithm finds singleton itemsets that meet a minimum support threshold, then generates and tests candidate itemsets of increasing size to find those that are frequent. The final frequent itemsets found are ['I1', 'I2', 'I3'] and ['I2', 'I5', 'I1'].

Uploaded by

Dreaming Boy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1/4/22, 11:37 AM Apriori Algorithm.

ipynb - Colaboratory

Apriori Algorithm

data = [
        ['T100',['I1','I2','I5']],
        ['T200',['I2','I4']],
        ['T300',['I2','I3']],
        ['T400',['I1','I2','I4']],
        ['T500',['I1','I3']],
        ['T600',['I2','I3']],
        ['T700',['I1','I3']],
        ['T800',['I1','I2','I3','I5']],
        ['T900',['I1','I2','I3']]
        ]
init = []
for i in data:
    for q in i[1]:
        if(q not in init):                      #39110242 D.Jeevana Aditya
            init.append(q)
init = sorted(init)
print(init)
sp = 0.4
s = int(sp*len(init))
s
from collections import Counter
c = Counter()
for i in init:
    for d in data:
        if(i in d[1]):
            c[i]+=1
print("C1:")
for i in c:
    print(str([i])+": "+str(c[i]))
print()
l = Counter()
for i in c:
    if(c[i] >= s):
        l[frozenset([i])]+=c[i]
print("L1:")
for i in l:
    print(str(list(i))+": "+str(l[i]))
print()
pl = l
pos = 1
for count in range (2,1000):
    nc = set()
    temp = list(l)
    for i in range(0,len(temp)):
        for j in range(i+1,len(temp)):
https://colab.research.google.com/drive/1CbvSQE-jWqC3THstECy98LmjykPiReib?usp=sharing#scrollTo=uoedWrR5xCOC 1/5
1/4/22, 11:37 AM Apriori Algorithm.ipynb - Colaboratory
         o  j    a ge( , e (te p)):
            t = temp[i].union(temp[j])
            if(len(t) == count):
                nc.add(temp[i].union(temp[j]))
    nc = list(nc)
    c = Counter()
    for i in nc:
        c[i] = 0
        for q in data:
            temp = set(q[1])
            if(i.issubset(temp)):
                c[i]+=1
    print("C"+str(count)+":")
    for i in c:
        print(str(list(i))+": "+str(c[i]))
    print()
    l = Counter()
    for i in c:
        if(c[i] >= s):
            l[i]+=c[i]
    print("L"+str(count)+":")
    for i in l:
        print(str(list(i))+": "+str(l[i]))
    print()
    if(len(l) == 0):
        break
    pl = l
    pos = count
print("Result: ")
print("L"+str(pos)+":")
for i in pl:
    print(str(list(i))+": "+str(pl[i]))
print()
from itertools import combinations
for l in pl:
    c = [frozenset(q) for q in combinations(l,len(l)-1)]
    mmax = 0
    for a in c:
        b = l-a
        ab = l
        sab = 0
        sa = 0
        sb = 0
        for q in data:
            temp = set(q[1])
            if(a.issubset(temp)):
                sa+=1
            if(b.issubset(temp)):
                sb+=1
            if(ab.issubset(temp)):
                sab+=1
        temp = sab/sa*100
if(t )
https://colab.research.google.com/drive/1CbvSQE-jWqC3THstECy98LmjykPiReib?usp=sharing#scrollTo=uoedWrR5xCOC 2/5
1/4/22, 11:37 AM Apriori Algorithm.ipynb - Colaboratory
        if(temp > mmax):
            mmax = temp
        temp = sab/sb*100
        if(temp > mmax):
            mmax = temp
        print(str(list(a))+" -> "+str(list(b))+" = "+str(sab/sa*100)+"%")
        print(str(list(b))+" -> "+str(list(a))+" = "+str(sab/sb*100)+"%")
    curr = 1
    print("choosing:", end=' ')
    for a in c:
        b = l-a
        ab = l
        sab = 0
        sa = 0
        sb = 0
        for q in data:
            temp = set(q[1])
            if(a.issubset(temp)):
                sa+=1
            if(b.issubset(temp)):
                sb+=1
            if(ab.issubset(temp)):
                sab+=1
        temp = sab/sa*100
        if(temp == mmax):
            print(curr, end = ' ')
        curr += 1
        temp = sab/sb*100
        if(temp == mmax):
            print(curr, end = ' ')
        curr += 1

['I1', 'I2', 'I3', 'I4', 'I5']

C1:

['I1']: 6

['I2']: 7

['I3']: 6

['I4']: 2

['I5']: 2

L1:

['I1']: 6

['I2']: 7

['I3']: 6

['I4']: 2

['I5']: 2

C2:

['I2', 'I5']: 2

['I1', 'I5']: 2

['I5', 'I3']: 1

['I4', 'I3']: 0

['I5', 'I4']: 0

https://colab.research.google.com/drive/1CbvSQE-jWqC3THstECy98LmjykPiReib?usp=sharing#scrollTo=uoedWrR5xCOC 3/5
1/4/22, 11:37 AM Apriori Algorithm.ipynb - Colaboratory

['I1', 'I3']: 4

['I2', 'I3']: 4

['I2', 'I4']: 2

['I1', 'I4']: 1

['I1', 'I2']: 4

L2:

['I2', 'I5']: 2

['I1', 'I5']: 2

['I1', 'I3']: 4

['I2', 'I3']: 4

['I2', 'I4']: 2

['I1', 'I2']: 4

C3:

['I2', 'I1', 'I4']: 1

['I2', 'I4', 'I3']: 0

['I2', 'I5', 'I3']: 1

['I1', 'I5', 'I3']: 1

['I2', 'I5', 'I4']: 0

['I1', 'I2', 'I3']: 2

['I2', 'I5', 'I1']: 2

L3:

['I1', 'I2', 'I3']: 2

['I2', 'I5', 'I1']: 2

C4:

['I1', 'I2', 'I5', 'I3']: 1

L4:

Result:

L3:

['I1', 'I2', 'I3']: 2

['I2', 'I5', 'I1']: 2

https://colab.research.google.com/drive/1CbvSQE-jWqC3THstECy98LmjykPiReib?usp=sharing#scrollTo=uoedWrR5xCOC 4/5
1/4/22, 11:37 AM Apriori Algorithm.ipynb - Colaboratory

https://colab.research.google.com/drive/1CbvSQE-jWqC3THstECy98LmjykPiReib?usp=sharing#scrollTo=uoedWrR5xCOC 5/5

You might also like