Code Monkey home page Code Monkey logo

python-fp-growth's Introduction

Python FP-Growth

This module provides a pure Python implementation of the FP-growth algorithm for finding frequent itemsets. FP-growth exploits an (often-valid) assumption that many transactions will have items in common to build a prefix tree. If the assumption holds true, this tree produces a compact representation of the actual transactions and is used to generate itemsets much faster than Apriori can.

Installation

After downloading and extracting the package, install the module by running python setup.py install from within the extracted package directory. (If you encounter errors, you may need to run setup with elevated permissions: sudo python setup.py install.)

Library Usage

Usage of the module is very simple. Assuming you have some iterable of transactions (which are themselves iterables of items) called transactions and an integer minimum support value minsup, you can find the frequent itemsets in your transactions with the following code:

from fp_growth import find_frequent_itemsets
for itemset in find_frequent_itemsets(transactions, minsup):
    print itemset

Note that find_frequent_itemsets returns a generator of itemsets, not a greedily-populated list. Each item must be hashable (i.e., it must be valid as a member of a dictionary or a set).

Script Usage

Once installed, the module can also be used as a stand-alone script. It will read a list of transactions formatted as a CSV file. (An example of such a file in included in the examples directory.)

python -m fp_growth -s {minimum support} {path to CSV file}

For example, to find the itemsets with support ≥ 4 in the included example file:

python -m fp_growth -s 4 examples/tsk.csv

References

The following references were used as source descriptions of the algorithm:

  • Tan, Pang-Ning, Michael Steinbach, and Vipin Kumar. Introduction to Data Mining. 1st ed. Boston: Pearson / Addison Wesley, 2006. (pp. 363-370)
  • Han, Jiawei, Jian Pei, and Yiwen Yin. "Mining Frequent Patterns without Candidate Generation." Proceedings of the 2000 ACM SIGMOD international conference on Management of data, 2000.

The example data included in tsk.csv comes from the section in Introduction to Data Mining.

License

The python-fp-growth package is made available under the terms of the MIT License.

Copyright © 2009 Eric Naeseth

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

python-fp-growth's People

Contributors

dzzh avatar enaeseth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-fp-growth's Issues

incorrect results when the number of distinct items is large

When the number of distinct items is larger than the max itemset size this library gives incorrect results.

I found this in two different ways: 1.) comparing against the brute-force method of computing all the subsets and 2.) checking that the apriori principle is satisfied by the results (i.e. the support of a superset should never be larger than the support of any of its subsets).

Here's a test:

#all combinations of length-4 from 20 different items
database = [frozenset(t) for t in itertools.combinations(range(20), r=4)]

fitemlists_w_sup = fp_growth.find_frequent_itemsets(database, minimum_support=2,
                                                    include_support=True)
fitemlists_w_sup = list(fitemlists_w_sup)  # I will reuse the generator

#brute force way calculate support
counts = []
for transaction in database:
    psets = powerset(transaction)
    psets = [pset for pset in psets if len(pset) > 0]  # remove empty set
    psets = map(frozenset, psets)
    counts.extend(psets)
counts = collections.Counter(counts)

#check that the brute-force method agrees with the fp-growth library
fpd = dict([(frozenset(x[0]), x[1]) for x in fitemlists_w_sup])
for fitemlist_w_sup in fitemlists_w_sup:
    if fitemlist_w_sup[1] != counts[frozenset(fitemlist_w_sup[0])]:
        print 'fp support is wrong. fp support: {0}, true support: {1}'\
            .format(fitemlist_w_sup, counts[frozenset(fitemlist_w_sup[0])])

        #check that the apriori principle is satisfied
        for subpset in powerset(fitemlist_w_sup[0]):
            if len(subpset) > 0:
                if fpd[frozenset(subpset)] < fpd[frozenset(fitemlist_w_sup[0])]:
                    print 'apriori?, subset and support: {0} {1}, superset and support: {2}'\
                        .format(subpset, fpd[frozenset(subpset)],fitemlist_w_sup)

For reference, here's how I calculated powerset:

def powerset(iterable):
    xs = list(iterable)
    return itertools.chain.from_iterable(
        itertools.combinations(xs, n) for n in range(max_size + 1))

Problem with the support values.

Using the following data set:
a,d,e
b,c,d
a,c,e
a,c,d,e
a,e
a,c,d
b,c
a,c,d,e
b,c,e
a,d,e

and modifying the find_with_suffix method in order to catch the support values for each itemset:
yield [found_set, support] instead of yield found_set

if you run the algorithm with a min_support value of 5, we have:
[['a'], 7]
[['c'], 7]
[['e'], 7]
[['a', 'e'], 6]
[['d'], 6]
[['a', 'd'], 7]
[['e', 'd'], 6]

Notice that the support value of the itemset {a,d} is 7. This value should be 5.

If you add a new transaction to the initial data set ( {b,c} ):
a,d,e
b,c,d
a,c,e
a,c,d,e
a,e
a,c,d
b,c
a,c,d,e
b,c,e
a,d,e
b,c ----> The new one

and running the algorithm with the same min_support value of 5, we obtain:
[['a'], 7]
[['c'], 8]
[['e'], 7]
[['a', 'e'], 9]
[['d'], 6]
[['a', 'd'], 5]
[['c', 'd'], 6]

Now, the support value for {a,d} is 5 (the correct value).

I'm not sure if I'm overlooking something, but it seems that something is not working properly.
Any idea about what could be wrong?

Thanks in advance.

Result is not correct for a specific case

Hi Eric,

Thanks for your nice work. I really love the code style and learn a lot from it.

I had a problem while using your library. Hope you can give me some help.

I use the following transaction as input.

a,d,e,f
a,d,e,f
a,c
a,c,d,e,f
a,c
a,c

The number of item set {a, e, f} is 3 but the function gives me 2 which is not correct.

I think the problem happens when you try to remove unpopular nodes in function conditional_tree_from_paths

Thanks for your work again.

Best,
Zicun

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.