From 6318e6f82ff472dbc376ec03180510ea6d134789 Mon Sep 17 00:00:00 2001 From: ymoch Date: Sat, 18 Aug 2018 14:06:06 +0900 Subject: [PATCH 1/4] Refactor. --- apyori.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/apyori.py b/apyori.py index 8f8bf3d..143fb46 100755 --- a/apyori.py +++ b/apyori.py @@ -142,11 +142,7 @@ def create_next_candidates(prev_candidates, length): length -- The lengths of the next candidates. """ # Solve the items. - item_set = set() - for candidate in prev_candidates: - for item in candidate: - item_set.add(item) - items = sorted(item_set) + items = sorted(frozenset(chain.from_iterable(prev_candidates))) # Create the temporary candidates. These will be filtered below. tmp_next_candidates = (frozenset(x) for x in combinations(items, length)) @@ -161,7 +157,7 @@ def create_next_candidates(prev_candidates, length): next_candidates = [ candidate for candidate in tmp_next_candidates if all( - True if frozenset(x) in prev_candidates else False + frozenset(x) in prev_candidates for x in combinations(candidate, length - 1)) ] return next_candidates From 4c9da5370f877af587fe730c1e3249ce2d302ccf Mon Sep 17 00:00:00 2001 From: ymoch Date: Sat, 30 Nov 2019 02:11:01 +0900 Subject: [PATCH 2/4] Fix ordered stats calculation. --- apyori.py | 18 ++++++++++-------- data/integration_test_output_1.txt | 10 +++++----- test/test_gen_ordered_statistics.py | 26 ++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/apyori.py b/apyori.py index 8f8bf3d..ba37c29 100755 --- a/apyori.py +++ b/apyori.py @@ -212,14 +212,16 @@ def gen_ordered_statistics(transaction_manager, record): record -- A support record as a SupportRecord instance. """ items = record.items - for combination_set in combinations(sorted(items), len(items) - 1): - items_base = frozenset(combination_set) - items_add = frozenset(items.difference(items_base)) - confidence = ( - record.support / transaction_manager.calc_support(items_base)) - lift = confidence / transaction_manager.calc_support(items_add) - yield OrderedStatistic( - frozenset(items_base), frozenset(items_add), confidence, lift) + sorted_items = sorted(items) + for base_length in range(len(items)): + for combination_set in combinations(sorted_items, base_length): + items_base = frozenset(combination_set) + items_add = frozenset(items.difference(items_base)) + confidence = ( + record.support / transaction_manager.calc_support(items_base)) + lift = confidence / transaction_manager.calc_support(items_add) + yield OrderedStatistic( + frozenset(items_base), frozenset(items_add), confidence, lift) def filter_ordered_statistics(ordered_statistics, **kwargs): diff --git a/data/integration_test_output_1.txt b/data/integration_test_output_1.txt index a1a7460..6305e1d 100644 --- a/data/integration_test_output_1.txt +++ b/data/integration_test_output_1.txt @@ -4,15 +4,15 @@ {"items": ["beer", "butter"], "support": 0.25, "ordered_statistics": [{"items_base": ["butter"], "items_add": ["beer"], "confidence": 0.6666666666666666, "lift": 1.0666666666666667}]} {"items": ["beer", "cheese"], "support": 0.25, "ordered_statistics": [{"items_base": ["cheese"], "items_add": ["beer"], "confidence": 0.6666666666666666, "lift": 1.0666666666666667}]} {"items": ["beer", "jam"], "support": 0.375, "ordered_statistics": [{"items_base": ["beer"], "items_add": ["jam"], "confidence": 0.6, "lift": 1.2}, {"items_base": ["jam"], "items_add": ["beer"], "confidence": 0.75, "lift": 1.2}]} -{"items": ["beer", "nuts"], "support": 0.5, "ordered_statistics": [{"items_base": ["beer"], "items_add": ["nuts"], "confidence": 0.8, "lift": 1.28}, {"items_base": ["nuts"], "items_add": ["beer"], "confidence": 0.8, "lift": 1.28}]} +{"items": ["beer", "nuts"], "support": 0.5, "ordered_statistics": [{"items_base": [], "items_add": ["beer", "nuts"], "confidence": 0.5, "lift": 1.0}, {"items_base": ["beer"], "items_add": ["nuts"], "confidence": 0.8, "lift": 1.28}, {"items_base": ["nuts"], "items_add": ["beer"], "confidence": 0.8, "lift": 1.28}]} {"items": ["cheese", "nuts"], "support": 0.375, "ordered_statistics": [{"items_base": ["cheese"], "items_add": ["nuts"], "confidence": 1.0, "lift": 1.6}, {"items_base": ["nuts"], "items_add": ["cheese"], "confidence": 0.6, "lift": 1.5999999999999999}]} {"items": ["jam", "nuts"], "support": 0.375, "ordered_statistics": [{"items_base": ["jam"], "items_add": ["nuts"], "confidence": 0.75, "lift": 1.2}, {"items_base": ["nuts"], "items_add": ["jam"], "confidence": 0.6, "lift": 1.2}]} {"items": ["beer", "butter", "jam"], "support": 0.125, "ordered_statistics": [{"items_base": ["beer", "butter"], "items_add": ["jam"], "confidence": 0.5, "lift": 1.0}, {"items_base": ["butter", "jam"], "items_add": ["beer"], "confidence": 1.0, "lift": 1.6}]} {"items": ["beer", "butter", "nuts"], "support": 0.125, "ordered_statistics": [{"items_base": ["beer", "butter"], "items_add": ["nuts"], "confidence": 0.5, "lift": 0.8}, {"items_base": ["butter", "nuts"], "items_add": ["beer"], "confidence": 1.0, "lift": 1.6}]} {"items": ["beer", "cheese", "jam"], "support": 0.125, "ordered_statistics": [{"items_base": ["beer", "cheese"], "items_add": ["jam"], "confidence": 0.5, "lift": 1.0}, {"items_base": ["cheese", "jam"], "items_add": ["beer"], "confidence": 1.0, "lift": 1.6}]} -{"items": ["beer", "cheese", "nuts"], "support": 0.25, "ordered_statistics": [{"items_base": ["beer", "cheese"], "items_add": ["nuts"], "confidence": 1.0, "lift": 1.6}, {"items_base": ["beer", "nuts"], "items_add": ["cheese"], "confidence": 0.5, "lift": 1.3333333333333333}, {"items_base": ["cheese", "nuts"], "items_add": ["beer"], "confidence": 0.6666666666666666, "lift": 1.0666666666666667}]} -{"items": ["beer", "jam", "nuts"], "support": 0.375, "ordered_statistics": [{"items_base": ["beer", "jam"], "items_add": ["nuts"], "confidence": 1.0, "lift": 1.6}, {"items_base": ["beer", "nuts"], "items_add": ["jam"], "confidence": 0.75, "lift": 1.5}, {"items_base": ["jam", "nuts"], "items_add": ["beer"], "confidence": 1.0, "lift": 1.6}]} +{"items": ["beer", "cheese", "nuts"], "support": 0.25, "ordered_statistics": [{"items_base": ["cheese"], "items_add": ["beer", "nuts"], "confidence": 0.6666666666666666, "lift": 1.3333333333333333}, {"items_base": ["beer", "cheese"], "items_add": ["nuts"], "confidence": 1.0, "lift": 1.6}, {"items_base": ["beer", "nuts"], "items_add": ["cheese"], "confidence": 0.5, "lift": 1.3333333333333333}, {"items_base": ["cheese", "nuts"], "items_add": ["beer"], "confidence": 0.6666666666666666, "lift": 1.0666666666666667}]} +{"items": ["beer", "jam", "nuts"], "support": 0.375, "ordered_statistics": [{"items_base": ["beer"], "items_add": ["jam", "nuts"], "confidence": 0.6, "lift": 1.5999999999999999}, {"items_base": ["jam"], "items_add": ["beer", "nuts"], "confidence": 0.75, "lift": 1.5}, {"items_base": ["nuts"], "items_add": ["beer", "jam"], "confidence": 0.6, "lift": 1.5999999999999999}, {"items_base": ["beer", "jam"], "items_add": ["nuts"], "confidence": 1.0, "lift": 1.6}, {"items_base": ["beer", "nuts"], "items_add": ["jam"], "confidence": 0.75, "lift": 1.5}, {"items_base": ["jam", "nuts"], "items_add": ["beer"], "confidence": 1.0, "lift": 1.6}]} {"items": ["butter", "jam", "nuts"], "support": 0.125, "ordered_statistics": [{"items_base": ["butter", "jam"], "items_add": ["nuts"], "confidence": 1.0, "lift": 1.6}, {"items_base": ["butter", "nuts"], "items_add": ["jam"], "confidence": 1.0, "lift": 2.0}]} {"items": ["cheese", "jam", "nuts"], "support": 0.125, "ordered_statistics": [{"items_base": ["cheese", "jam"], "items_add": ["nuts"], "confidence": 1.0, "lift": 1.6}]} -{"items": ["beer", "butter", "jam", "nuts"], "support": 0.125, "ordered_statistics": [{"items_base": ["beer", "butter", "jam"], "items_add": ["nuts"], "confidence": 1.0, "lift": 1.6}, {"items_base": ["beer", "butter", "nuts"], "items_add": ["jam"], "confidence": 1.0, "lift": 2.0}, {"items_base": ["butter", "jam", "nuts"], "items_add": ["beer"], "confidence": 1.0, "lift": 1.6}]} -{"items": ["beer", "cheese", "jam", "nuts"], "support": 0.125, "ordered_statistics": [{"items_base": ["beer", "cheese", "jam"], "items_add": ["nuts"], "confidence": 1.0, "lift": 1.6}, {"items_base": ["beer", "cheese", "nuts"], "items_add": ["jam"], "confidence": 0.5, "lift": 1.0}, {"items_base": ["cheese", "jam", "nuts"], "items_add": ["beer"], "confidence": 1.0, "lift": 1.6}]} +{"items": ["beer", "butter", "jam", "nuts"], "support": 0.125, "ordered_statistics": [{"items_base": ["beer", "butter"], "items_add": ["jam", "nuts"], "confidence": 0.5, "lift": 1.3333333333333333}, {"items_base": ["butter", "jam"], "items_add": ["beer", "nuts"], "confidence": 1.0, "lift": 2.0}, {"items_base": ["butter", "nuts"], "items_add": ["beer", "jam"], "confidence": 1.0, "lift": 2.6666666666666665}, {"items_base": ["beer", "butter", "jam"], "items_add": ["nuts"], "confidence": 1.0, "lift": 1.6}, {"items_base": ["beer", "butter", "nuts"], "items_add": ["jam"], "confidence": 1.0, "lift": 2.0}, {"items_base": ["butter", "jam", "nuts"], "items_add": ["beer"], "confidence": 1.0, "lift": 1.6}]} +{"items": ["beer", "cheese", "jam", "nuts"], "support": 0.125, "ordered_statistics": [{"items_base": ["beer", "cheese"], "items_add": ["jam", "nuts"], "confidence": 0.5, "lift": 1.3333333333333333}, {"items_base": ["cheese", "jam"], "items_add": ["beer", "nuts"], "confidence": 1.0, "lift": 2.0}, {"items_base": ["beer", "cheese", "jam"], "items_add": ["nuts"], "confidence": 1.0, "lift": 1.6}, {"items_base": ["beer", "cheese", "nuts"], "items_add": ["jam"], "confidence": 0.5, "lift": 1.0}, {"items_base": ["cheese", "jam", "nuts"], "items_add": ["beer"], "confidence": 1.0, "lift": 1.6}]} diff --git a/test/test_gen_ordered_statistics.py b/test/test_gen_ordered_statistics.py index e9f2ce5..0163af6 100644 --- a/test/test_gen_ordered_statistics.py +++ b/test/test_gen_ordered_statistics.py @@ -17,17 +17,43 @@ def test_normal(): """ transaction_manager = Mock(spec=TransactionManager) transaction_manager.calc_support.side_effect = lambda key: { + frozenset([]): 1.0, frozenset(['A']): 0.8, frozenset(['B']): 0.4, frozenset(['C']): 0.2, frozenset(['A', 'B']): 0.2, frozenset(['A', 'C']): 0.1, frozenset(['B', 'C']): 0.01, + frozenset(['A', 'B', 'C']): 0.001, }.get(key, 0.0) test_data = SupportRecord(frozenset(['A', 'B', 'C']), 0.001) results = list(gen_ordered_statistics(transaction_manager, test_data)) eq_(results, [ + OrderedStatistic( + frozenset([]), + frozenset(['A', 'B', 'C']), + 0.001 / 1.0, + 0.001 / 1.0 / 0.001, + ), + OrderedStatistic( + frozenset(['A']), + frozenset(['B', 'C']), + 0.001 / 0.8, + 0.001 / 0.8 / 0.01, + ), + OrderedStatistic( + frozenset(['B']), + frozenset(['A', 'C']), + 0.001 / 0.4, + 0.001 / 0.4 / 0.1, + ), + OrderedStatistic( + frozenset(['C']), + frozenset(['A', 'B']), + 0.001 / 0.2, + 0.001 / 0.2 / 0.2, + ), OrderedStatistic( frozenset(['A', 'B']), frozenset(['C']), From 961471af93d1a74b35a56535269cacdb5c4c451a Mon Sep 17 00:00:00 2001 From: ymoch Date: Sat, 30 Nov 2019 02:16:38 +0900 Subject: [PATCH 3/4] Fix build errors. --- .travis.yml | 3 +-- dev-requirements.txt | 2 +- setup.py | 1 - 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3f07da1..2558081 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: python python: - 2.7 - - 3.3 - 3.4 - 3.5 install: @@ -12,7 +11,7 @@ script: # Normal unit tests. - coverage run --source=apyori setup.py test # Code quality check. - - pylint apyori.py test/*.py + - pyflakes apyori.py test/*.py # Integration test - apyori-run data/integration_test_input_1.tsv > result.txt - diff result.txt data/integration_test_output_1.txt diff --git a/dev-requirements.txt b/dev-requirements.txt index c5e61f2..69e60b7 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -3,4 +3,4 @@ nose mock # Code quality utilities. -pylint +pyflakes diff --git a/setup.py b/setup.py index 433cdc4..f7eca6d 100755 --- a/setup.py +++ b/setup.py @@ -32,7 +32,6 @@ 'License :: OSI Approved :: MIT License', 'Programming Language :: Python', 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3.3', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', 'Topic :: Scientific/Engineering :: Mathematics', From 399c13718fba43d71aa3c7b7f80e7d7dd2b807e9 Mon Sep 17 00:00:00 2001 From: ymoch Date: Sat, 30 Nov 2019 02:24:16 +0900 Subject: [PATCH 4/4] Update to 1.1.2. --- apyori.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apyori.py b/apyori.py index ab66ace..b618e63 100755 --- a/apyori.py +++ b/apyori.py @@ -15,7 +15,7 @@ # Meta informations. -__version__ = '1.1.1' +__version__ = '1.1.2' __author__ = 'Yu Mochizuki' __author_email__ = 'ymoch.dev@gmail.com'