Professional Documents
Culture Documents
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)
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
(Coke,
10 (Milk) 0.4 0.8 0.4 1.000000 1.250000 0.08
Diaper)
(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)
(Coke,
15 (Milk) 0.8 0.4 0.4 0.500000 1.250000 0.08
Diaper)
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 [ ]:
color_map=[]
N = 50
colors = np.random.rand(N)
strs=['R0', 'R1', 'R2', 'R3', 'R4', 'R5', 'R6', 'R7', 'R8', 'R9', 'R10', 'R11']
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)
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]
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