pandas 数据框的先验规则
Apriori rule to pandas dataframe
我有以下问题。我正在使用 python 中的 efficient_apriori
包进行关联规则挖掘。我想将我的规则保存为 pandas 数据框。看我的代码:
for rule in rules:
dict = {
"left" : [str(rule.lhs).replace(",)",")")],
"right" : [str(rule.rhs).replace(",)",")")],
"support" : [str(rule.support)],
"confidence" : [str(rule.confidence)]
}
df = pd.DataFrame.from_dict(dict)
还有比这更好的方法吗?
# this output after print(rule)
{Book1} -> {Book2} (conf: 0.541, supp: 0.057, lift: 4.417, conv: 1.914)
# this output after print(type(rule))
<class 'efficient_apriori.rules.Rule'>
使用 Rule
个实例的内部 __dict__
:
设置一个MRE
# Sample from documentation
from efficient_apriori import apriori
transactions = [('eggs', 'bacon', 'soup'),
('eggs', 'bacon', 'apple'),
('soup', 'bacon', 'banana')]
itemsets, rules = apriori(transactions, min_support=0.5, min_confidence=1)
一些检查
>>> rules
[{eggs} -> {bacon}, {soup} -> {bacon}]
>>> str(rules[0])
'{eggs} -> {bacon} (conf: 1.000, supp: 0.667, lift: 1.000, conv: 0.000)'
>>> type(rules[0])
efficient_apriori.rules.Rule
>>> pd.DataFrame([rule.__dict__ for rule in rules])
lhs rhs count_full count_lhs count_rhs num_transactions
0 (eggs,) (bacon,) 2 2 3 3
1 (soup,) (bacon,) 2 2 3 3
更新
I would like to save also support and confidence.
data = [dict(**rule.__dict__, confidence=rule.confidence, support=rule.support)
for rule in rules]
df = pd.DataFrame(data)
print(df)
# Output:
lhs rhs count_full count_lhs count_rhs num_transactions confidence support
0 (eggs,) (bacon,) 2 2 3 3 1.0 0.666667
1 (soup,) (bacon,) 2 2 3 3 1.0 0.666667
我有以下问题。我正在使用 python 中的 efficient_apriori
包进行关联规则挖掘。我想将我的规则保存为 pandas 数据框。看我的代码:
for rule in rules:
dict = {
"left" : [str(rule.lhs).replace(",)",")")],
"right" : [str(rule.rhs).replace(",)",")")],
"support" : [str(rule.support)],
"confidence" : [str(rule.confidence)]
}
df = pd.DataFrame.from_dict(dict)
还有比这更好的方法吗?
# this output after print(rule)
{Book1} -> {Book2} (conf: 0.541, supp: 0.057, lift: 4.417, conv: 1.914)
# this output after print(type(rule))
<class 'efficient_apriori.rules.Rule'>
使用 Rule
个实例的内部 __dict__
:
设置一个MRE
# Sample from documentation
from efficient_apriori import apriori
transactions = [('eggs', 'bacon', 'soup'),
('eggs', 'bacon', 'apple'),
('soup', 'bacon', 'banana')]
itemsets, rules = apriori(transactions, min_support=0.5, min_confidence=1)
一些检查
>>> rules
[{eggs} -> {bacon}, {soup} -> {bacon}]
>>> str(rules[0])
'{eggs} -> {bacon} (conf: 1.000, supp: 0.667, lift: 1.000, conv: 0.000)'
>>> type(rules[0])
efficient_apriori.rules.Rule
>>> pd.DataFrame([rule.__dict__ for rule in rules])
lhs rhs count_full count_lhs count_rhs num_transactions
0 (eggs,) (bacon,) 2 2 3 3
1 (soup,) (bacon,) 2 2 3 3
更新
I would like to save also support and confidence.
data = [dict(**rule.__dict__, confidence=rule.confidence, support=rule.support)
for rule in rules]
df = pd.DataFrame(data)
print(df)
# Output:
lhs rhs count_full count_lhs count_rhs num_transactions confidence support
0 (eggs,) (bacon,) 2 2 3 3 1.0 0.666667
1 (soup,) (bacon,) 2 2 3 3 1.0 0.666667