1 2 3 4 5 | from apyori import load_transactions with open('path_to_file') as f: transactions = load_transactions(f) |
1 2 | with open('path_to_file') as f: transactions = list(load_transactions(f)) |
Note: Avoid using syntax such as load_transaction(‘/path/to/file’). To maintain flexibility to accept path-like objects, such syntax will behave unexpectedly.
1 2 3 | # To load from a csv with open('path_to_file') as f: transactions = load_transactions(f, delimiter=",") |
1 | apriori(transactions) |
1 2 3 4 5 | apriori(transactions, min_support=0.02, min_confidence=0.80, min_lift=1.0, max_length=None) |
1 2 3 4 5 | list(apriori(transactions, min_support=0.02, min_confidence=0.80, min_lift=1.0, max_length=None)) |
Full Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | from apyori import apriori, load_transactions """ data.csv contains the following data: >>> [['beer', 'nuts'], >>> ['beer', 'cheese']] """ with open('data.csv') as f: transactions = load_transactions(f, delimiter=",") results = list(apriori(transactions, min_confidence=0.8)) print(results) [RelationRecord(items=frozenset({'beer'}), support=1.0, ordered_statistics=[OrderedStatistic(items_base=frozenset(), items_add=frozenset({'beer'}), confidence=1.0, lift=1.0)]), RelationRecord(items=frozenset({'cheese', 'beer'}), support=0.5, ordered_statistics=[OrderedStatistic(items_base=frozenset({'cheese'}), items_add=frozenset({'beer'}), confidence=1.0, lift=1.0)]), RelationRecord(items=frozenset({'beer', 'nuts'}), support=0.5, ordered_statistics=[OrderedStatistic(items_base=frozenset({'nuts'}), items_add=frozenset({'beer'}), confidence=1.0, lift=1.0)])] |
1 2 3 4 5 6 7 | from apyori import apriori transactions = [ ['beer', 'nuts'], ['beer', 'cheese'], ] results = list(apriori(transactions)) |
1 2 3 4 5 6 7 8 9 10 | RelationRecord(items=frozenset({'beer', 'nuts'}), support=0.5, ordered_statistics=[OrderedStatistic(items_base=frozenset({'beer'}), items_add=frozenset({'nuts'}), confidence=0.5, lift=1.0), OrderedStatistic(items_base=frozenset({'nuts'}), items_add=frozenset({'beer'}), confidence=1.0, lift=1.0)]) |