Skip to main content

An efficient Python implementation of the Apriori algorithm.

Reason this release was yanked:

Error - .py files were not included

Project description

Efficient-Apriori Build Status PyPI version Documentation Status Downloads Black

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 described in the original paper by Agrawal et al, published in 1994.

The code is stable and in widespread use. It's cited in the book "Mastering Machine Learning Algorithms" by Bonaccorso.

The code is fast. See timings in this PR.

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

If your data is in a pandas DataFrame, you must convert it to a list of tuples. Do you have missing values, or does the algorithm run for a long time? See this comment. More examples are included below.

Installation

The software is available through GitHub, and through PyPI. You may install the software using pip.

pip install efficient-apriori

Contributing

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

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

Transactions with IDs

If you need to know which transactions occurred in the frequent itemsets, set the output_transaction_ids parameter to True. This changes the output to contain ItemsetCount objects for each itemset. The objects have a members property containing is the set of ids of frequent transactions as well as a count property. The ids are the enumeration of the transactions in the order they appear.

from efficient_apriori import apriori
transactions = [('eggs', 'bacon', 'soup'),
                ('eggs', 'bacon', 'apple'),
                ('soup', 'bacon', 'banana')]
itemsets, rules = apriori(transactions, output_transaction_ids=True)
print(itemsets)
# {1: {('bacon',): ItemsetCount(itemset_count=3, members={0, 1, 2}), ...

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-2.0.4.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

efficient_apriori-2.0.4-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: efficient_apriori-2.0.4.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for efficient_apriori-2.0.4.tar.gz
Algorithm Hash digest
SHA256 2b8bb15cbd9d915f78f877a6c75e291ac18ef54ba01ca81bb950abfeda135491
MD5 6d38aeb8d4722914a4b60593f917ab42
BLAKE2b-256 54aae68ba3811739fecf2ae8b3c0751c166543877bd332c7fd443ac18c98e096

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for efficient_apriori-2.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 da4d12371cf77d19fad9595d9122b56f858382dfe586abbae8836439f35d9f40
MD5 189b0da730243d88f5c782d389efb87e
BLAKE2b-256 5bdd8610a600e23a3a199deb72d0f3d004f171648ec0f372bddc6379eeb8d6c2

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