-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEclat.py
36 lines (28 loc) · 1.19 KB
/
Eclat.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
# Eclat Algorithm tutorial from Machine Learning A-Z - SuperDataScience -> Input by Ryan L Buchanan 12OCT20
!pip install apyori
# Import libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Data Preprocessing
dataset = pd.read_csv('Market_Basket_Optimisation.csv', header = None)
transactions = []
for i in range(0, 7501):
transactions.append([str(dataset.values[i, j]) for j in range(0, 20)])
# Train the Eclat model on the dataset
from apyori import apriori
rules = apriori(transactions, min_support = 0.003, min_confidence = 0.2, min_lift = 3, min_length = 2,
max_length = 2)
# Visualze the results
results = list(rules)
print(results)
# Put the results into a well-organized Pandas DataFrame
def inspect(results):
lhs = [tuple(result[2][0][0])[0] for result in results]
rhs = [tuple(result[2][0][1])[0] for result in results]
supports = [result[1] for result in results]
return list(zip(lhs, rhs, supports))
resultsinDataFrame = pd.DataFrame(inspect(results), columns = ['Product 1', 'Product 2', 'Support'])
print(resultsinDataFrame)
# Display the results sorted by descending lifts
resultsinDataFrame.nlargest(n = 10, columns = 'Support')