Skip to main content

An efficient Python implementation of the Apriori algorithm.

Project description

Efficient-Apriori Build Status

An efficient pure Python implementation of the Apriori algorithm.

The apriori algorithm uncovers hidden structures in categorical data. The classical example is a database containing purchases from a supermarket. Every purchase has a number of items associated with it. We would like to uncover association rules such as {bread, eggs} -> {bacon} from the data. This is the goal of association rule learning, and the Apriori algorithm is arguably the most famous algorithm for this problem. This repository contains an efficient, well-tested implementation of the apriori algorithm as descriped in the original paper by Agrawal et al, published in 1994.

Example

Here's a minimal working example. Notice that in every transaction with eggs present, bacon is present too. Therefore, the rule {eggs} -> {bacon} is returned with 100 % confidence.

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)
print(rules)  # [{eggs} -> {bacon}, {soup} -> {bacon}]

More examples are included below.

Installation

Here's how to install from GitHub.

git clone https://github.com/tommyod/Efficient-Apriori.git
cd Efficient-Apriori
pip install .

Contributing

You are very welcome to scrutinize the code and make pull requests if you have suggestions for improvements. Your submitted code must be PEP8 compliant, and all tests must pass.

More examples

Filtering and sorting association rules

It's possible to filter and sort the returned list of association rules.

from efficient_apriori import apriori
transactions = [('eggs', 'bacon', 'soup'),
                ('eggs', 'bacon', 'apple'),
                ('soup', 'bacon', 'banana')]
itemsets, rules = apriori(transactions, min_support=0.2,  min_confidence=1)

# Print out every rule with 2 items on the left hand side,
# 1 item on the right hand side, sorted by lift
rules_rhs = filter(lambda rule: len(rule.lhs) == 2 and len(rule.rhs) == 1, rules)
for rule in sorted(rules_rhs, key=lambda rule: rule.lift):
  print(rule) # Prints the rule and its confidence, support, lift, ...

Working with large datasets

If you have data that is too large to fit into memory, you may pass a function returning a generator instead of a list. The min_support will most likely have to be a large value, or the algorithm will take very long before it terminates. If you have massive amounts of data, this Python implementation is likely not fast enough, and you should consult more specialized implementations.

def data_generator(filename):
  """
  Data generator, needs to return a generator to be called several times.
  """
  def data_gen():
    with open(filename) as file:
      for line in file:
        yield tuple(k.strip() for k in line.split(','))      

  return data_gen

transactions = data_generator('dataset.csv')
itemsets, rules = apriori(transactions, min_support=0.9, min_confidence=0.6)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

efficient_apriori-0.4.3.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

efficient_apriori-0.4.3-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file efficient_apriori-0.4.3.tar.gz.

File metadata

File hashes

Hashes for efficient_apriori-0.4.3.tar.gz
Algorithm Hash digest
SHA256 1a281ff8ed7ea291454c2d62c9967df9ba5db028b66fa2020e6dd0b2a3b62872
MD5 7beefcc0491f2756b7c3d5c72a90a4bc
BLAKE2b-256 08a07ae6f52006272ac334e7a4411639915862496cc62dc4ee1254f57283b60f

See more details on using hashes here.

File details

Details for the file efficient_apriori-0.4.3-py3-none-any.whl.

File metadata

File hashes

Hashes for efficient_apriori-0.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2ef9d7ab32b136cb1c49a1b388a7d8237a135a1d460229efaaad04923366daf0
MD5 d7eb0d901d5961025e282482cb4aa834
BLAKE2b-256 0d1ae305972fd3edd80a7ae534c35d89f5ed437665fc1db0627f590a48ea16e8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page