Apriori Algorithm Apriori Algorithm: Problem Statement Problem Statement

You might also like

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

Apriori Algorithm

Problem Statement
Attempt is made to understand the choices of users, which channels have similar content and can be
recomended to users based on their history.

Dataset : The dataset contains transactions where each transaction is a list of channels that users of lastfm
listened one after other within a short time span. Datatset Link:
https://www.biz.uiowa.edu/faculty/jledolter/DataMining/lastfm.csv

1.Import Required Packages

In [1]:
!pip install apyori
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt
from apyori import apriori
%matplotlib inline
import os
print(os.listdir("../input"))

Collecting apyori
Downloading https://files.pythonhosted.org/packages/5e/62/5ffde5c473ea4b033490617ec5caa
80d59804875ad3c3c57c0976533a21a/apyori-1.1.2.tar.gz
Building wheels for collected packages: apyori
Building wheel for apyori (setup.py) ... done
Stored in directory: /root/.cache/pip/wheels/5d/92/bb/474bbadbc8c0062b9eb168f69982a0443
263f8ab1711a8cad0
Successfully built apyori
Installing collected packages: apyori
Successfully installed apyori-1.1.2
[]

2.Load Dataset

In [2]:
lastfm1 = pd.read_csv("https://www.biz.uiowa.edu/faculty/jledolter/DataMining/lastfm.csv"
)

3.Generate Shallow Copy

In [3]:

lastfm = lastfm1.copy()
lastfm.shape
Out[3]:
(289955, 4)

4.Differentiate Data and Extract useful columns


In [4]:

lastfm = lastfm[['user','artist']]

5.Drop Duplicate Data

In [5]:
lastfm = lastfm.drop_duplicates()
lastfm.shape

Out[5]:
(289953, 2)

6.Transform Dataset into form of Transaction into list

In [6]:
records = []
for i in lastfm['user'].unique():
records.append(list(lastfm[lastfm['user'] == i]['artist'].values))

In [7]:

print(type(records))

<class 'list'>

7.Generate Rule using Apriori Algorithm

In [8]:
association_rules = apriori(records, min_support=0.01, min_confidence=0.4, min_lift=3, m
in_length=2)
association_results = list(association_rules)

In [9]:
print("There are {} Relation derived.".format(len(association_results)))

There are 91 Relation derived.

8.Display All Rules

In [10]:
for i in range(0, len(association_results)):
print(association_results[i][0])

frozenset({'tool', 'a perfect circle'})


frozenset({'kaiser chiefs', 'arctic monkeys'})
frozenset({'beyoncé', 'rihanna'})
frozenset({'metallica', 'black sabbath'})
frozenset({'blink-182', 'sum 41'})
frozenset({'breaking benjamin', 'linkin park'})
frozenset({'bright eyes', 'death cab for cutie'})
frozenset({'broken social scene', 'death cab for cutie'})
frozenset({'broken social scene', 'radiohead'})
frozenset({'children of bodom', 'in flames'})
frozenset({'keane', 'coldplay'})
frozenset({'snow patrol', 'coldplay'})
frozenset({'the fray', 'coldplay'})
frozenset({'travis', 'coldplay'})
frozenset({'daft punk', 'justice'})
frozenset({'death cab for cutie', 'the decemberists'})
frozenset({'death cab for cutie', 'the decemberists'})
frozenset({'death cab for cutie', 'the postal service'})
frozenset({'death cab for cutie', 'the shins'})
frozenset({'led zeppelin', 'deep purple'})
frozenset({'metallica', 'dream theater'})
frozenset({'panic at the disco', 'fall out boy'})
frozenset({'kaiser chiefs', 'franz ferdinand'})
frozenset({'linkin park', 'good charlotte'})
frozenset({'metallica', 'guns n roses'})
frozenset({'judas priest', 'iron maiden'})
frozenset({'megadeth', 'iron maiden'})
frozenset({'metallica', 'iron maiden'})
frozenset({'jay-z', 'kanye west'})
frozenset({'jay-z', 'nas'})
frozenset({'jimi hendrix', 'led zeppelin'})
frozenset({'judas priest', 'metallica'})
frozenset({'kaiser chiefs', 'the killers'})
frozenset({'kanye west', 'nas'})
frozenset({'kanye west', 't.i.'})
frozenset({'the killers', 'keane'})
frozenset({'koЯn', 'system of a down'})
frozenset({'kylie minogue', 'madonna'})
frozenset({'led zeppelin', 'pink floyd'})
frozenset({'linkin park', 'limp bizkit'})
frozenset({'linkin park', 'papa roach'})
frozenset({'metallica', 'megadeth'})
frozenset({'metallica', 'motörhead'})
frozenset({'metallica', 'pantera'})
frozenset({'metallica', 'slayer'})
frozenset({'the smiths', 'morrissey'})
frozenset({'nightwish', 'sonata arctica'})
frozenset({'rihanna', 'the pussycat dolls'})
frozenset({'system of a down', 'slipknot'})
frozenset({'muse', 'arctic monkeys', 'coldplay'})
frozenset({'radiohead', 'arctic monkeys', 'coldplay'})
frozenset({'the killers', 'arctic monkeys', 'coldplay'})
frozenset({'muse', 'radiohead', 'arctic monkeys'})
frozenset({'muse', 'the killers', 'arctic monkeys'})
frozenset({'beck', 'the beatles', 'radiohead'})
frozenset({'the killers', 'bloc party', 'coldplay'})
frozenset({'bob dylan', 'pink floyd', 'the beatles'})
frozenset({'bob dylan', 'radiohead', 'the beatles'})
frozenset({'bob dylan', 'the rolling stones', 'the beatles'})
frozenset({'the killers', 'death cab for cutie', 'coldplay'})
frozenset({'red hot chili peppers', 'foo fighters', 'coldplay'})
frozenset({'the killers', 'franz ferdinand', 'coldplay'})
frozenset({'placebo', 'muse', 'coldplay'})
frozenset({'muse', 'the killers', 'coldplay'})
frozenset({'red hot chili peppers', 'nirvana', 'coldplay'})
frozenset({'oasis', 'radiohead', 'coldplay'})
frozenset({'the beatles', 'oasis', 'coldplay'})
frozenset({'the killers', 'oasis', 'coldplay'})
frozenset({'sigur rós', 'radiohead', 'coldplay'})
frozenset({'radiohead', 'snow patrol', 'coldplay'})
frozenset({'the killers', 'radiohead', 'coldplay'})
frozenset({'radiohead', 'the smashing pumpkins', 'coldplay'})
frozenset({'u2', 'radiohead', 'coldplay'})
frozenset({'the killers', 'red hot chili peppers', 'coldplay'})
frozenset({'the killers', 'snow patrol', 'coldplay'})
frozenset({'the beatles', 'the killers', 'coldplay'})
frozenset({'david bowie', 'pink floyd', 'the beatles'})
frozenset({'david bowie', 'the beatles', 'the rolling stones'})
frozenset({'the shins', 'death cab for cutie', 'radiohead'})
frozenset({'led zeppelin', 'pink floyd', 'radiohead'})
frozenset({'led zeppelin', 'pink floyd', 'the beatles'})
frozenset({'led zeppelin', 'pink floyd', 'the doors'})
frozenset({'led zeppelin', 'the beatles', 'the rolling stones'})
frozenset({'metallica', 'red hot chili peppers', 'nirvana'})
frozenset({'muse', 'radiohead', 'placebo'})
frozenset({'muse', 'radiohead', 'the killers'})
frozenset({'pink floyd', 'the doors', 'the beatles'})
frozenset({'the beatles', 'sigur rós', 'radiohead'})
frozenset({'the beatles', 'the rolling stones', 'radiohead'})
frozenset({'the beatles', 'the rolling stones', 'radiohead'})
frozenset({'the beatles', 'the shins', 'radiohead'})
frozenset({'the beatles', 'radiohead', 'the smashing pumpkins'})
frozenset({'the beatles', 'radiohead', 'the strokes'})

9.Final Result with Support, Confidense and Lift

In [11]:
for item in association_results:
# first index of the inner list
# Contains base item and add item
pair = item[0]
items = [x for x in pair]
print("Rule: With " + items[0] + " you can also listen " + items[1])

# second index of the inner list


print("Support: " + str(item[1]))

# third index of the list located at 0th


# of the third index of the inner list

print("Confidence: " + str(item[2][0][2]))


print("Lift: " + str(item[2][0][3]))
print("=====================================")

Rule: With tool you can also listen a perfect circle


Support: 0.016266666666666665
Confidence: 0.44283121597096187
Lift: 8.717149920688225
=====================================
Rule: With kaiser chiefs you can also listen arctic monkeys
Support: 0.012533333333333334
Confidence: 0.4008528784648188
Lift: 5.3116547499755145
=====================================
Rule: With beyoncé you can also listen rihanna
Support: 0.013933333333333334
Confidence: 0.46860986547085204
Lift: 10.88103402796096
=====================================
Rule: With metallica you can also listen black sabbath
Support: 0.0172
Confidence: 0.45263157894736844
Lift: 4.06555310431768
=====================================
Rule: With blink-182 you can also listen sum 41
Support: 0.014133333333333333
Confidence: 0.42741935483870963
Lift: 7.420474910394264
=====================================
Rule: With breaking benjamin you can also listen linkin park
Support: 0.0108
Confidence: 0.4426229508196721
Lift: 4.507362024640246
=====================================
Rule: With bright eyes you can also listen death cab for cutie
Support: 0.0152
Confidence: 0.4021164021164021
Lift: 4.944054124381993
=====================================
Rule: With broken social scene you can also listen death cab for cutie
Support: 0.011466666666666667
Confidence: 0.41646489104116224
Lift: 5.120469971817569
=====================================
Rule: With broken social scene you can also listen radiohead
Support: 0.015066666666666667
Confidence: 0.5472154963680388
Lift: 3.0355889221599788
=====================================
Rule: With children of bodom you can also listen in flames
Rule: With children of bodom you can also listen in flames
Support: 0.013
Confidence: 0.4012345679012346
Lift: 8.111210941399621
=====================================
Rule: With keane you can also listen coldplay
Support: 0.022266666666666667
Confidence: 0.6374045801526719
Lift: 4.020634441669503
=====================================
Rule: With snow patrol you can also listen coldplay
Support: 0.026466666666666666
Confidence: 0.5251322751322751
Lift: 3.312440759875579
=====================================
Rule: With the fray you can also listen coldplay
Support: 0.011266666666666666
Confidence: 0.5168195718654434
Lift: 3.2600057098324853
=====================================
Rule: With travis you can also listen coldplay
Support: 0.013733333333333334
Confidence: 0.5628415300546448
Lift: 3.5503040163245045
=====================================
Rule: With daft punk you can also listen justice
Support: 0.014533333333333334
Confidence: 0.42661448140900193
Lift: 5.613348439592131
=====================================
Rule: With death cab for cutie you can also listen the decemberists
Support: 0.013466666666666667
Confidence: 0.42348008385744235
Lift: 5.206722342509537
=====================================
Rule: With death cab for cutie you can also listen the postal service
Support: 0.015333333333333332
Confidence: 0.46938775510204084
Lift: 5.771160923385748
=====================================
Rule: With death cab for cutie you can also listen the shins
Support: 0.02
Confidence: 0.4048582995951417
Lift: 4.977765978628792
=====================================
Rule: With led zeppelin you can also listen deep purple
Support: 0.010866666666666667
Confidence: 0.4024691358024691
Lift: 5.0816810076069325
=====================================
Rule: With metallica you can also listen dream theater
Support: 0.013666666666666667
Confidence: 0.4124748490945674
Lift: 3.70486391402306
=====================================
Rule: With panic at the disco you can also listen fall out boy
Support: 0.011533333333333333
Confidence: 0.43467336683417085
Lift: 8.413032906467823
=====================================
Rule: With kaiser chiefs you can also listen franz ferdinand
Support: 0.0132
Confidence: 0.4221748400852879
Lift: 7.115306293572267
=====================================
Rule: With linkin park you can also listen good charlotte
Support: 0.010133333333333333
Confidence: 0.40533333333333327
Lift: 4.127630685675491
=====================================
Rule: With metallica you can also listen guns n roses
Support: 0.011866666666666666
Confidence: 0.419811320754717
Confidence: 0.419811320754717
Lift: 3.770760366060332
=====================================
Rule: With judas priest you can also listen iron maiden
Support: 0.013533333333333333
Confidence: 0.5075
Lift: 8.56299212598425
=====================================
Rule: With megadeth you can also listen iron maiden
Support: 0.013266666666666666
Confidence: 0.4307359307359307
Lift: 7.26776036112369
=====================================
Rule: With metallica you can also listen iron maiden
Support: 0.0296
Confidence: 0.49943757030371205
Lift: 4.485966200332743
=====================================
Rule: With jay-z you can also listen kanye west
Support: 0.015066666666666667
Confidence: 0.4967032967032967
Lift: 7.752913059884965
=====================================
Rule: With jay-z you can also listen nas
Support: 0.0106
Confidence: 0.4262734584450402
Lift: 14.052971157528798
=====================================
Rule: With jimi hendrix you can also listen led zeppelin
Support: 0.017
Confidence: 0.4314720812182742
Lift: 5.447879813362047
=====================================
Rule: With judas priest you can also listen metallica
Support: 0.013133333333333334
Confidence: 0.4925
Lift: 4.423652694610778
=====================================
Rule: With kaiser chiefs you can also listen the killers
Support: 0.015066666666666667
Confidence: 0.4818763326226013
Lift: 4.907090963570279
=====================================
Rule: With kanye west you can also listen nas
Support: 0.0104
Confidence: 0.41823056300268097
Lift: 6.528052492237476
=====================================
Rule: With kanye west you can also listen t.i.
Support: 0.0104
Confidence: 0.5672727272727273
Lift: 8.854413016743923
=====================================
Rule: With the killers you can also listen keane
Support: 0.014066666666666667
Confidence: 0.40267175572519087
Lift: 4.100527044044714
=====================================
Rule: With koЯn you can also listen system of a down
Support: 0.0214
Confidence: 0.43437077131258456
Lift: 4.773305179259171
=====================================
Rule: With kylie minogue you can also listen madonna
Support: 0.010933333333333333
Confidence: 0.478134110787172
Lift: 8.757034996102051
=====================================
Rule: With led zeppelin you can also listen pink floyd
Support: 0.032
Confidence: 0.40404040404040403
Lift: 3.8504485772592507
=====================================
=====================================
Rule: With linkin park you can also listen limp bizkit
Support: 0.0128
Confidence: 0.4507042253521127
Lift: 4.589656062648806
=====================================
Rule: With linkin park you can also listen papa roach
Support: 0.013133333333333334
Confidence: 0.4712918660287081
Lift: 4.799306171371773
=====================================
Rule: With metallica you can also listen megadeth
Support: 0.016266666666666665
Confidence: 0.5281385281385281
Lift: 4.743759234777198
=====================================
Rule: With metallica you can also listen motörhead
Support: 0.010133333333333333
Confidence: 0.4293785310734463
Lift: 3.856693392875266
=====================================
Rule: With metallica you can also listen pantera
Support: 0.012466666666666666
Confidence: 0.45945945945945943
Lift: 4.126881372390354
=====================================
Rule: With metallica you can also listen slayer
Support: 0.015933333333333334
Confidence: 0.47420634920634924
Lift: 4.259338465925293
=====================================
Rule: With the smiths you can also listen morrissey
Support: 0.011266666666666666
Confidence: 0.465564738292011
Lift: 8.896141496025688
=====================================
Rule: With nightwish you can also listen sonata arctica
Support: 0.013466666666666667
Confidence: 0.51010101010101
Lift: 8.236291874612649
=====================================
Rule: With rihanna you can also listen the pussycat dolls
Support: 0.0104
Confidence: 0.5777777777777778
Lift: 13.415892672858618
=====================================
Rule: With system of a down you can also listen slipknot
Support: 0.018533333333333332
Confidence: 0.41124260355029585
Lift: 4.519149489563691
=====================================
Rule: With muse you can also listen arctic monkeys
Support: 0.0116
Confidence: 0.4275184275184275
Lift: 3.747969849664765
=====================================
Rule: With radiohead you can also listen arctic monkeys
Support: 0.013066666666666667
Confidence: 0.48877805486284287
Lift: 3.0831248204132224
=====================================
Rule: With the killers you can also listen arctic monkeys
Support: 0.012333333333333333
Confidence: 0.4545454545454546
Lift: 4.628772449546381
=====================================
Rule: With muse you can also listen radiohead
Support: 0.011
Confidence: 0.41147132169576056
Lift: 3.607288033568912
=====================================
Rule: With muse you can also listen the killers
Support: 0.011066666666666667
Support: 0.011066666666666667
Confidence: 0.46368715083798884
Lift: 4.721865079816587
=====================================
Rule: With beck you can also listen the beatles
Support: 0.013
Confidence: 0.5909090909090909
Lift: 3.2779720279720284
=====================================
Rule: With the killers you can also listen bloc party
Support: 0.011066666666666667
Confidence: 0.4811594202898551
Lift: 4.899790430650255
=====================================
Rule: With bob dylan you can also listen pink floyd
Support: 0.010333333333333333
Confidence: 0.6150793650793651
Lift: 3.4580923823802387
=====================================
Rule: With bob dylan you can also listen radiohead
Support: 0.013866666666666666
Confidence: 0.5730027548209367
Lift: 3.2215297310022675
=====================================
Rule: With bob dylan you can also listen the rolling stones
Support: 0.011466666666666667
Confidence: 0.5910652920962199
Lift: 3.3230807276773984
=====================================
Rule: With the killers you can also listen death cab for cutie
Support: 0.010866666666666667
Confidence: 0.5884476534296029
Lift: 3.711822876973946
=====================================
Rule: With red hot chili peppers you can also listen foo fighters
Support: 0.010733333333333333
Confidence: 0.4535211267605634
Lift: 3.808968029903948
=====================================
Rule: With the killers you can also listen franz ferdinand
Support: 0.0106
Confidence: 0.4922600619195046
Lift: 5.012831587774996
=====================================
Rule: With placebo you can also listen muse
Support: 0.0112
Confidence: 0.47323943661971835
Lift: 4.148796931207349
=====================================
Rule: With muse you can also listen the killers
Support: 0.015133333333333334
Confidence: 0.5089686098654709
Lift: 3.2104832413717674
=====================================
Rule: With red hot chili peppers you can also listen nirvana
Support: 0.0104
Confidence: 0.4444444444444444
Lift: 3.7327360955580438
=====================================
Rule: With oasis you can also listen radiohead
Support: 0.012733333333333333
Confidence: 0.5876923076923076
Lift: 3.7070582907420584
=====================================
Rule: With the beatles you can also listen oasis
Support: 0.0106
Confidence: 0.5196078431372548
Lift: 3.277593627863256
=====================================
Rule: With the killers you can also listen oasis
Support: 0.011133333333333334
Confidence: 0.42710997442455245
Lift: 4.349388741594221
Lift: 4.349388741594221
=====================================
Rule: With sigur rós you can also listen radiohead
Support: 0.012066666666666667
Confidence: 0.5801282051282052
Lift: 3.2181668183887124
=====================================
Rule: With radiohead you can also listen snow patrol
Support: 0.010066666666666666
Confidence: 0.634453781512605
Lift: 4.002021329978585
=====================================
Rule: With the killers you can also listen radiohead
Support: 0.015066666666666667
Confidence: 0.5243619489559165
Lift: 3.307581679705108
=====================================
Rule: With radiohead you can also listen the smashing pumpkins
Support: 0.010933333333333333
Confidence: 0.628352490421456
Lift: 3.4856831939060062
=====================================
Rule: With u2 you can also listen radiohead
Support: 0.0114
Confidence: 0.5213414634146342
Lift: 3.288528995466574
=====================================
Rule: With the killers you can also listen red hot chili peppers
Support: 0.010866666666666667
Confidence: 0.509375
Lift: 3.213046677880572
=====================================
Rule: With the killers you can also listen snow patrol
Support: 0.0104
Confidence: 0.5954198473282443
Lift: 3.7558022329367806
=====================================
Rule: With the beatles you can also listen the killers
Support: 0.012533333333333334
Confidence: 0.5340909090909091
Lift: 3.368950225552412
=====================================
Rule: With david bowie you can also listen pink floyd
Support: 0.010066666666666666
Confidence: 0.5741444866920151
Lift: 3.2279487632609545
=====================================
Rule: With david bowie you can also listen the beatles
Support: 0.01
Confidence: 0.5703422053231939
Lift: 3.206571619133399
=====================================
Rule: With the shins you can also listen death cab for cutie
Support: 0.010066666666666666
Confidence: 0.4441176470588235
Lift: 5.46046287367406
=====================================
Rule: With led zeppelin you can also listen pink floyd
Support: 0.0112
Confidence: 0.4528301886792453
Lift: 4.315408405456594
=====================================
Rule: With led zeppelin you can also listen pink floyd
Support: 0.0156
Confidence: 0.46613545816733065
Lift: 4.442205763983456
=====================================
Rule: With led zeppelin you can also listen pink floyd
Support: 0.010666666666666666
Confidence: 0.5970149253731343
Lift: 5.689468793263668
=====================================
Rule: With led zeppelin you can also listen the beatles
Rule: With led zeppelin you can also listen the beatles
Support: 0.010666666666666666
Confidence: 0.5776173285198556
Lift: 3.2474737360561594
=====================================
Rule: With metallica you can also listen red hot chili peppers
Support: 0.0104
Confidence: 0.41160949868073876
Lift: 3.4569666742503253
=====================================
Rule: With muse you can also listen radiohead
Support: 0.013666666666666667
Confidence: 0.5137844611528822
Lift: 4.504247175507443
=====================================
Rule: With muse you can also listen radiohead
Support: 0.0128
Confidence: 0.4454756380510441
Lift: 3.905397177536915
=====================================
Rule: With pink floyd you can also listen the doors
Support: 0.01
Confidence: 0.42735042735042733
Lift: 4.072589841331899
=====================================
Rule: With the beatles you can also listen sigur rós
Support: 0.010466666666666668
Confidence: 0.6434426229508198
Lift: 3.5693932486177133
=====================================
Rule: With the beatles you can also listen the rolling stones
Support: 0.0106
Confidence: 0.5638297872340425
Lift: 3.169957574404287
=====================================
Rule: With the beatles you can also listen the shins
Support: 0.010666666666666666
Confidence: 0.5673758865248226
Lift: 3.1474254060178772
=====================================
Rule: With the beatles you can also listen radiohead
Support: 0.011466666666666667
Confidence: 0.6209386281588448
Lift: 3.444555999401876
=====================================
Rule: With the beatles you can also listen radiohead
Support: 0.010466666666666668
Confidence: 0.5607142857142857
Lift: 3.1104712595097213
=====================================

You might also like