An efficient Python implementation of the Apriori algorithm.
Project description
Efficient-Apriori
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file efficient_apriori-0.4.2.tar.gz
.
File metadata
- Download URL: efficient_apriori-0.4.2.tar.gz
- Upload date:
- Size: 11.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b11c9de2132abf20d21732bb67a7783f9c13541a28704eb25699d98c5faf6672 |
|
MD5 | 90fab1d736d752b8f67c465d11d34a4b |
|
BLAKE2b-256 | c3813c4de681e682d923205e8170622b1847640d3cf0dfd01db9d42b9b0fd535 |
File details
Details for the file efficient_apriori-0.4.2-py3-none-any.whl
.
File metadata
- Download URL: efficient_apriori-0.4.2-py3-none-any.whl
- Upload date:
- Size: 12.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ce44535ff1dc9cea021b34b84ab5da256487951917c4a6683f85a13d127fd9a |
|
MD5 | fc92bde624c6ce5cc96a7027229e2032 |
|
BLAKE2b-256 | 9c2742deae293343e559f78168c919dd951b8287d4f7747d98e85edde4ed593d |