-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarket_basket_analysis.py
36 lines (26 loc) · 1.01 KB
/
market_basket_analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import numpy as np
import pandas as pd
from apyori import apriori
# import the dataset
store_data = pd.read_csv('./data/store_data.csv',header = None)
# Data preprocessing
records = []
for i in range(0, 7501):
records.append([str(store_data.values[i,j]) for j in range(0, 20)])
# Apply Apriori algorithm
association_rules = apriori(records, min_support=0.005, min_confidence=0.2, min_lift=3, min_length=2)
association_results = list(association_rules)
# Interpretation with output
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: " + items[0] + " -> " + 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("=====================================")