Home > Software engineering >  Apriori rule to pandas dataframe
Apriori rule to pandas dataframe

Time:12-07

I have a following problem. I am doing association rules mining using efficient_apriori package in python. I would like to save my rules as pandas data frame. See my code:

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)

Is there a better way than this?

# 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'>

CodePudding user response:

Use internal __dict__ of Rule instance:

Setup a 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)

Some checks

>>> 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

Update

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
  • Related