Skip to content

Commit 4efeb8c

Browse files
committed
Alternate implementation
1 parent 96dfd4d commit 4efeb8c

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

mlxtend/frequent_patterns/apriori.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#
55
# License: BSD 3 clause
66

7+
import itertools
78
import numpy as np
89
import pandas as pd
910
from ..frequent_patterns import fpcommon as fpc
@@ -42,17 +43,26 @@ def generate_new_combinations(old_combinations):
4243
4344
"""
4445

45-
length = len(old_combinations)
46-
for i, old_combination in enumerate(old_combinations):
47-
*head_i, _ = old_combination
48-
j = i + 1
49-
while j < length:
50-
*head_j, tail_j = old_combinations[j]
51-
if head_i != head_j:
52-
break
53-
yield from old_combination
54-
yield tail_j
55-
j = j + 1
46+
if old_combinations.shape[1] == 1:
47+
for pair in itertools.combinations(old_combinations.reshape(-1), 2):
48+
yield from pair
49+
else:
50+
length = len(old_combinations)
51+
i = 0
52+
while i < length - 1:
53+
*head_i, tail_i = old_combinations[i]
54+
tails = [tail_i]
55+
j = i + 1
56+
while j < length:
57+
*head_j, tail_j = old_combinations[j]
58+
if head_i != head_j:
59+
break
60+
tails.append(tail_j)
61+
j = j + 1
62+
for pair in itertools.combinations(tails, 2):
63+
yield from head_i
64+
yield from pair
65+
i = j
5666

5767

5868
def generate_new_combinations_low_memory(old_combinations, X, min_support,

0 commit comments

Comments
 (0)