Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Frequent pattern mining in Python

I want to know how to get the absolute support and relative support of itemsets in python. Presently I have the following:

import pandas as  pd
import pyfpgrowth
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori
from collections import Counter


dataset = [['a', 'b', 'c', 'd'],
              ['b', 'c', 'e', 'f'],
              ['a', 'd', 'e', 'f'],
              ['a', 'e', 'f'],
              ['b', 'd', 'f']
           ]
te = TransactionEncoder()
te_ary = te.fit(dataset).transform(dataset)
df = pd.DataFrame(te_ary, columns=te.columns_)
print (df)
#print support
print(apriori(df, min_support = 0.0))
#print frequent itemset
frequent_itemsets = apriori(df, min_support=0.6, use_colnames=True)
frequent_itemsets['length'] = frequent_itemsets['itemsets'].apply(lambda x: 
len(x))
frequent_itemsets
print ("frequent itemset at min support = 0.6")
print(frequent_itemsets)

but I do not know how to return the absolute support and relative support.

like image 283
olu Avatar asked Aug 07 '26 01:08

olu


1 Answers

The relative support is part of your frequen_itemsets DataFrame. You can get it from:

frequent_itemsets['support']

And you can calculate the absolute support multiplying support by the number of baskets:

frequent_itemsets['support']*len(dataset)
like image 133
Franco Piccolo Avatar answered Aug 10 '26 02:08

Franco Piccolo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!