'Modifying an Apriori program in Python

I am having an issue with my Apriori program here:

import time

def load_data_set():
    data_set = [
        ['A', 'B', 'C', 'D', 'F', 'G', 'H'],
        ['A', 'B', 'C', 'D'],
        ['A', 'B', 'C', 'D'],
        ['A', 'B', 'D'],
        ['B', 'C', 'E']
    ]
    return data_set


def create_C1(data_set):
    C1 = set()
    for t in data_set:
        for item in t:
            item_set = frozenset([item])
            C1.add(item_set)
    return C1


def is_apriori(Ck_item, Lksub1):
    for item in Ck_item:
        sub_Ck = Ck_item - frozenset([item])
        if sub_Ck not in Lksub1:
            return False
    return True


def create_Ck(Lksub1, k):
    Ck = set()
    len_Lksub1 = len(Lksub1)
    list_Lksub1 = list(Lksub1)
    for i in range(len_Lksub1):
        for j in range(1, len_Lksub1):
            l1 = list(list_Lksub1[i])
            l2 = list(list_Lksub1[j])
            l1.sort()
            l2.sort()
            if l1[0:k-2] == l2[0:k-2]:
                Ck_item = list_Lksub1[i] | list_Lksub1[j]
                if is_apriori(Ck_item, Lksub1):
                    Ck.add(Ck_item)
    return Ck


def generate_Lk_by_Ck(data_set, Ck, min_support, support_data):
    Lk = set()
    item_count = {}
    for t in data_set:
        for item in Ck:
            if item.issubset(t):
                if item not in item_count:
                    item_count[item] = 1
                else:
                    item_count[item] += 1
    t_num = float(len(data_set))
    for item in item_count:
        if (item_count[item] / t_num) >= min_support:
            Lk.add(item)
            support_data[item] = item_count[item] / t_num
    return Lk


def generate_L(data_set, k, min_support):
    support_data = {}
    C1 = create_C1(data_set)
    L1 = generate_Lk_by_Ck(data_set, C1, min_support, support_data)
    Lksub1 = L1.copy()
    L = []
    L.append(Lksub1)
    for i in range(2, k+1):
        Ci = create_Ck(Lksub1, i)
        Li = generate_Lk_by_Ck(data_set, Ci, min_support, support_data)
        Lksub1 = Li.copy()
        L.append(Lksub1)
    return L, support_data


def generate_big_rules(L, support_data, min_conf, min_lift):
    big_rule_list = []
    sub_set_list = []
    for i in range(0, len(L)):
        for freq_set in L[i]:
            for sub_set in sub_set_list:
                if sub_set.issubset(freq_set):
                    conf = support_data[freq_set] / support_data[freq_set - sub_set]
                    lift = conf/support_data[freq_set]
                    support = min(support_data[freq_set], support_data[freq_set - sub_set])
                    big_rule = (freq_set - sub_set, sub_set, lift, conf, support)
                    if conf >= min_conf and lift>= min_lift and big_rule not in big_rule_list:
                        big_rule_list.append(big_rule)
            sub_set_list.append(freq_set)
    return big_rule_list

if __name__ == "__main__":
    start = time.time()
    data_set = load_data_set()
    L, support_data = generate_L(data_set, k=4, min_support=0.25)
    big_rules_list = generate_big_rules(L, support_data, min_conf=0.80, min_lift=1)
    print()
    print ("Association Rules:")
    print()
    for item in big_rules_list:
        print (item[0], "=>", item[1], item[2], item[3], item[4])

This worked on my loaded data set; as you can see in the program, I've set the k = 4 so that it runs up until the frequent 4-items sets, as I know that this dataset only has up until the frequent 4-items sets. However, in real life scenarios with other data sets, this might not be the case.

How do I implement and modify my current program in a way, let's say we set k=1, then we repeat until no frequent item sets are identified instead of setting a specific k-value straight away like my current program?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source