Professional Documents
Culture Documents
Apriori Algorithm Apriori Algorithm: Problem Statement Problem Statement
Apriori Algorithm Apriori Algorithm: Problem Statement Problem Statement
Apriori Algorithm Apriori Algorithm: Problem Statement Problem Statement
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
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"
)
In [3]:
lastfm = lastfm1.copy()
lastfm.shape
Out[3]:
(289955, 4)
lastfm = lastfm[['user','artist']]
In [5]:
lastfm = lastfm.drop_duplicates()
lastfm.shape
Out[5]:
(289953, 2)
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'>
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)))
In [10]:
for i in range(0, len(association_results)):
print(association_results[i][0])
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])