-
Notifications
You must be signed in to change notification settings - Fork 0
/
apriori_association_rule_learning_big_basket_cart_prediction.py
57 lines (37 loc) · 1.48 KB
/
apriori_association_rule_learning_big_basket_cart_prediction.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# -*- coding: utf-8 -*-
"""Apriori_Association_Rule_Learning_Big-Basket-Cart-Prediction.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1hFGPfS20Yu9CXKmGov2BvYLklkSldTzo
# Apriori
## Importing libraries
"""
!pip install apyori
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
"""## Importing dataset"""
dataset = pd.read_csv('Big Basket.com Cart.csv', header = None)
transactions = []
for i in range(0, 7219):
transactions.append([str(dataset.values[i,j]) for j in range(0, 20)])
"""## Apriori Training on Dataset"""
from apyori import apriori
rules = apriori(transactions = transactions, min_support = 0.003, min_confidence = 0.2, min_lift = 3, min_length = 2, max_length = 2)
"""## Visualizing
### Raw Results
"""
results = list(rules)
results
print(results)
"""### Proper Display"""
def inspect(results):
product1 = [tuple(result[2][0][0])[0] for result in results]
product2 = [tuple(result[2][0][1])[0] for result in results]
supports = [result[1] for result in results]
confidences = [result[2][0][2] for result in results]
lifts = [result[2][0][3] for result in results]
return list(zip(product1, product2, supports, confidences, lifts))
DataFrame_intelligence = pd.DataFrame(inspect(results), columns = ['product1', 'product1', 'Support', 'Confidence', 'Lift'])
DataFrame_intelligence
DataFrame_intelligence.nlargest(n = 10, columns = 'Lift')