You are on page 1of 4

16:56 06/04/2024 2054020061_LeChiBao_Trực_quan_hóa_luật_kết_hợp

Lê Chí Bảo

2054020061

Load Dataset
In [23]: import pandas as pd
import numpy as np
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori, association_rules

dataset = []
with open('/content/drive/MyDrive/Colab Notebooks/example.txt') as file:
for line in file:
transaction = line.split()
dataset.append(transaction)

te = TransactionEncoder()
te_ary = te.fit(dataset).transform(dataset)
df = pd.DataFrame(te_ary, columns=te.columns_)
print(df)

frequent_itemsets = apriori(df, min_support=0.4, use_colnames=True)


print(frequent_itemsets)

rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.7


rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1.2)
rules

Beer Bread Coke Diaper Eggs Milk


0 False True False False False True
1 True True False True True False
2 True False True True False True
3 True True False True False True
4 False True True True False True
support itemsets
0 0.6 (Beer)
1 0.8 (Bread)
2 0.4 (Coke)
3 0.8 (Diaper)
4 0.8 (Milk)
5 0.4 (Bread, Beer)
6 0.6 (Beer, Diaper)
7 0.4 (Beer, Milk)
8 0.6 (Bread, Diaper)
9 0.6 (Bread, Milk)
10 0.4 (Coke, Diaper)
11 0.4 (Coke, Milk)
12 0.6 (Diaper, Milk)
13 0.4 (Bread, Beer, Diaper)
14 0.4 (Beer, Diaper, Milk)
15 0.4 (Bread, Diaper, Milk)
16 0.4 (Coke, Diaper, Milk)

localhost:8888/nbconvert/html/Downloads/2054020061_LeChiBao_Trực_quan_hóa_luật_kết_hợp.ipynb?download=false 1/4
16:56 06/04/2024 2054020061_LeChiBao_Trực_quan_hóa_luật_kết_hợp

Out[23]: antecedent consequent


antecedents consequents support confidence lift leverage
support support

0 (Beer) (Diaper) 0.6 0.8 0.6 1.000000 1.250000 0.12

1 (Diaper) (Beer) 0.8 0.6 0.6 0.750000 1.250000 0.12

2 (Coke) (Diaper) 0.4 0.8 0.4 1.000000 1.250000 0.08

3 (Diaper) (Coke) 0.8 0.4 0.4 0.500000 1.250000 0.08

4 (Coke) (Milk) 0.4 0.8 0.4 1.000000 1.250000 0.08

5 (Milk) (Coke) 0.8 0.4 0.4 0.500000 1.250000 0.08

6 (Bread, Beer) (Diaper) 0.4 0.8 0.4 1.000000 1.250000 0.08

7 (Diaper) (Bread, Beer) 0.8 0.4 0.4 0.500000 1.250000 0.08

8 (Beer, Milk) (Diaper) 0.4 0.8 0.4 1.000000 1.250000 0.08

9 (Diaper) (Beer, Milk) 0.8 0.4 0.4 0.500000 1.250000 0.08

(Coke,
10 (Milk) 0.4 0.8 0.4 1.000000 1.250000 0.08
Diaper)

11 (Coke, Milk) (Diaper) 0.4 0.8 0.4 1.000000 1.250000 0.08

(Diaper,
12 (Coke) 0.6 0.4 0.4 0.666667 1.666667 0.16
Milk)

(Diaper,
13 (Coke) 0.4 0.6 0.4 1.000000 1.666667 0.16
Milk)

14 (Diaper) (Coke, Milk) 0.8 0.4 0.4 0.500000 1.250000 0.08

(Coke,
15 (Milk) 0.8 0.4 0.4 0.500000 1.250000 0.08
Diaper)

In [24]: support = rules['support'].to_numpy()


confidence = rules['confidence'].to_numpy()

In [25]: import random


import matplotlib.pyplot as plt

for i in range (len(support)):


support[i] = support[i] + 0.0025 * (random.randint(1,10) - 5)
confidence[i] = confidence[i] + 0.0025 * (random.randint(1,10) - 5)

plt.scatter(support, confidence, alpha=0.5, marker="*")


plt.xlabel('support')
plt.ylabel('confidence')
plt.show()

localhost:8888/nbconvert/html/Downloads/2054020061_LeChiBao_Trực_quan_hóa_luật_kết_hợp.ipynb?download=false 2/4
16:56 06/04/2024 2054020061_LeChiBao_Trực_quan_hóa_luật_kết_hợp

In [ ]:

Trực quan hóa kết hợp


In [26]: def draw_graph(rules, rules_to_show):
import networkx as nx
G1 = nx.DiGraph()

color_map=[]
N = 50
colors = np.random.rand(N)
strs=['R0', 'R1', 'R2', 'R3', 'R4', 'R5', 'R6', 'R7', 'R8', 'R9', 'R10', 'R11']

for i in range (rules_to_show):


G1.add_nodes_from(["R"+str(i)])

for a in rules.iloc[i]['antecedents']:
G1.add_nodes_from([a])
G1.add_edge(a, "R"+str(i), color=colors[i] , weight = 2)

for c in rules.iloc[i]['consequents']:
G1.add_nodes_from([c])
G1.add_edge("R"+str(i), c, color=colors[i], weight=2)

for node in G1:


found_a_string = False
for item in strs:
if node==item:
found_a_string = True
if found_a_string:
color_map.append('yellow')
else:
color_map.append('green')

localhost:8888/nbconvert/html/Downloads/2054020061_LeChiBao_Trực_quan_hóa_luật_kết_hợp.ipynb?download=false 3/4
16:56 06/04/2024 2054020061_LeChiBao_Trực_quan_hóa_luật_kết_hợp
edges = G1.edges()
colors = [G1[u][v]['color'] for u,v in edges]
weights = [G1[u][v]['weight'] for u,v in edges]

pos = nx.spring_layout(G1, k=16, scale=1)


#nx.draw(G1, pos, edges=edges, node_color=color_map, edge_color=colors, width=w
nx.draw(G1, pos, node_color=color_map, edge_color=colors, width=weights, font_s

for p in pos: # raise text positions


pos[p][1] += 0.07
nx.draw_networkx_labels(G1, pos)
plt.show()

In [29]: '''draw_graph(rules,6)'''
rules_to_show = min(10, len(rules))
draw_graph(rules, rules_to_show)

localhost:8888/nbconvert/html/Downloads/2054020061_LeChiBao_Trực_quan_hóa_luật_kết_hợp.ipynb?download=false 4/4

You might also like