0
$\begingroup$

I have a dataset:

[['Apple', 'Beer', 'Chicken', 'Rice'],
['Apple', 'Beer', 'Rice'],
['Apple', 'Beer'],
['Apple', 'Bananas']]

I am using the frequent itemsets tools, specifically apriori, to find frequent datasets. I convert to a OneHotDataset, then to a Data Frame:

oht = OnehotTransactions()
oht_ary = oht.fit(tempList).transform(tempList)
oht_ary

df = pd.DataFrame(oht_ary, columns=oht.columns_)
df

Then I use the Apriori function:

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

I see this table, which is what I expect:

support                      itemsets
0      1.00                       [Apple]
1      0.25                     [Bananas]
2      0.75                        [Beer]
3      0.25                     [Chicken]
4      0.50                        [Rice]
5      0.25              [Apple, Bananas]
6      0.75                 [Apple, Beer]
7      0.25              [Apple, Chicken]
8      0.50                 [Apple, Rice]
9      0.25               [Beer, Chicken]
10     0.50                  [Beer, Rice]
11     0.25               [Chicken, Rice]
12     0.25        [Apple, Beer, Chicken]
13     0.50           [Apple, Beer, Rice]
14     0.25        [Apple, Chicken, Rice]
15     0.25         [Beer, Chicken, Rice]
16     0.25  [Apple, Beer, Chicken, Rice]

The question I have is, is there any way to print a suppport "count" of occurrences of the datasets-meaning how many times that itemset appears in the transactions? One thing I messed around with from the mlxtend site was this being added to add an additional column of length but couldn't get a count piece:

frequent_itemsets['length'] = frequent_itemsets['itemsets'].apply(lambda x: len(x))

EDIT: Ok I think I am confusing folks. What I would like to see is:

      support     itemsets    count
0      1.00       [Apple]       4
1      0.25      [Bananas]      1
2      0.75         [Beer]      3
3      0.25       [Chicken]     1

I guess I could just create a column that uses the total support and multiplies by the relative support, but was looking for a more systematic way to do it.

$\endgroup$
4
  • $\begingroup$ Do you want to count how often an item set occurs in the dataset? That would be from collections import Counter Counter(tempList) $\endgroup$ – Elias Strehle Feb 15 '18 at 17:20
  • $\begingroup$ I tried to use this but got "List is not hashable" $\endgroup$ – Vaslo Feb 15 '18 at 18:14
  • 2
    $\begingroup$ Make it a tuple and then use the Counter $\endgroup$ – Kiritee Gak Feb 15 '18 at 18:21
  • 1
    $\begingroup$ The problem is that lists are mutable. That's why you need to cast them to tuples. $\endgroup$ – Emre Feb 15 '18 at 18:59
1
$\begingroup$

consider the master dataframe on which the apriori algorithm is applied as df1

ls1=frequent_itemsets['support']*len(df1) frequent_itemsets.insert(loc=2,column='Count',value=ls1)

Now count is appended in your frequent_itemsets

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.