Example #1
0
    def test_all_rules(self):
        analyzer = GCA(self.db_RAR, 0.25)
        analyzer.clean_database()
        analyzer.mine()

        lattice = analyzer.lcg_into_lattice()
        rule_miner = RAMMax(analyzer.lcg_into_list())

        nb_rules = 0
        nb_basic_rules = 0
        for node in lattice.values():
            S = node.fci
            print('S: ' + str(S.closure))

            to_extract = deque()
            to_extract.append(node)
            to_extract.extend(node.children)
            visited = deque()
            while len(to_extract) > 0:
                current = to_extract.popleft()
                visited.append(current)
                L = current.fci

                RAR = rule_miner.mine_RAR(L, S, 0.95, 1.0, 0.95, 1.0)
                nb_consequent = len(rule_miner.mine_CAR2(L, S, RAR, analyzer))
                nb_basic_rules += len(RAR)
                nb_rules += nb_consequent

                print('  - L:' + str(L.closure) + ',gen: ' +
                      str(L.generators) + ', nb BR min/max: ' + str(len(RAR)) +
                      ', nb CR: ' + str(nb_consequent) + ', TBR: ' +
                      str(nb_basic_rules) + ', TBC: ' + str(nb_rules))

                for child in current.children:
                    for grandchild in child.children:
                        if grandchild not in to_extract and grandchild not in visited:
                            to_extract.append(grandchild)
                        else:
                            print('Child: ' + str(grandchild.fci.closure) +
                                  ', gen: ' + str(grandchild.fci.generators) +
                                  ' already waiting for extraction or visited')

        print('nb rules: ' + str(nb_rules))
        self.assertTrue(False)
Example #2
0
def use_reference(file):
    db = []
    with open('./../data/' + file + '.dat') as csvfile:
        reader = csvfile.read()
        rows = reader.split('\n')
        for row in rows:
            transaction = row.split(' ')
            db.append(transaction)

    min_support = 0.95
    analyzer = GCA(db, min_support)
    analyzer.mine()
    frequent_items = analyzer.lcg_into_list()

    lattice = analyzer.lcg_into_lattice()

    nb_frequent_items = len(frequent_items)
    print('Nb frequent items with min_support = ' + str(min_support) + ': ' +
          str(nb_frequent_items))

    rule_miner = RAMMax(analyzer.lcg_into_list())
    #rule_miner = RAMin(analyzer.lcg_into_list())

    nb_rules = 0
    nb_basic_rules = 0
    print('Extract rules from frequent items: ')

    rules = deque()
    for node in lattice.values():
        S = node.fci
        print('S: ' + str(S.closure))

        to_extract = deque()
        to_extract.append(node)
        visited = deque()
        while len(to_extract) > 0:
            current = to_extract.popleft()
            visited.append(current)
            L = current.fci

            #RAR = rule_miner.mine_basic(L, S)
            RAR = rule_miner.mine_RAR(L, S, 0.95, 1.0, 0.95, 1.0)
            ne_rules = rule_miner.mine_CAR2(L, S, RAR, analyzer)

            is_new_rule = True
            for new_rule in ne_rules:
                for saved_rules in rules:
                    if new_rule.left == saved_rules.left and new_rule.right == saved_rules.right:
                        is_new_rule = False
                        break
                if is_new_rule:
                    rules.append(new_rule)
                    nb_rules += 1

            #nb_basic_rules += len(RAR)

            print('  - L:' + str(L.closure) + ',gen: ' + str(L.generators) +
                  ', nb BR min/max: ' + str(len(RAR)) + ', TBR: ' +
                  str(nb_basic_rules) + ', TBC: ' + str(nb_rules))
            #print(' - L:' + str(L.closure) + ',gen: ' + str(L.generators) + ', nb BR min/max: ' + str(len(RAR)) + ', TBR: ' + str(nb_basic_rules))
            for rule in RAR:
                print('  - ' + rule.to_str())

            for child in current.children:
                for grandchild in child.children:
                    if grandchild not in to_extract and grandchild not in visited:
                        to_extract.append(grandchild)

    print('nb rules: ' + str(nb_basic_rules))
Example #3
0
def find_closed_items():
    print('Load deck')
    card_loader = MagicLoader()
    card_loader.load('./../data/magic_cards/AllCards-x.json')

    print('Clean deck')
    deck_loader = DeckManager()

    list_files = os.listdir("./../data/decks_mtgdeck_net")
    for i in range(len(list_files)):  # returns list
        list_files[i] = './../data/decks_mtgdeck_net/' + list_files[i]

    deck_loader.load_from_mtgdeck_csv(list_files, card_loader)
    deck_loader.extract_lands(card_loader.lands, card_loader)

    analyzer = GCA(deck_loader.decks, 0.05)

    print('Start mining ' + str(len(deck_loader.decks)) + ' decks')
    analyzer.mine()
    print('nb closed items = ' + str(len(analyzer.lcg_into_list())))
    #deck_loader.write_frequent_items_into_csv('genclose_results', analyzer.get_closed_items_closures(), card_loader)

    frequent_items = analyzer.lcg_into_list()
    lattice = analyzer.lcg_into_lattice()

    generated_rules = []
    '''
    rule_miner = RAMCM(frequent_items)
    for pair in combinations(list(range(nb_frequent_items)), 2):
        L1 = frequent_items[pair[0]].closure
        R1 = frequent_items[pair[1]].closure
        rule_miner.mine(0.3, 1.0, 0.33, 1.0, L1, R1)
        generated_rules.extend(rule_miner.ars)
    '''

    rule_miner = RAMMax(analyzer.lcg_into_list())

    nb_rules = 0
    nb_basic_rules = 0
    print('Extract rules from frequent items: ')

    for node in lattice.values():
        S = node.fci
        #print('S: ' + str(S.closure))

        to_extract = deque()
        to_extract.append(node)
        to_extract.extend(node.children)
        visited = deque()
        while len(to_extract) > 0:
            current = to_extract.popleft()
            visited.append(current)
            L = current.fci

            RAR = rule_miner.mine_RAR(L, S, 0.05, 0.08, 0.7, 0.9)
            '''
            nb_consequent = len(rule_miner.mine_CAR2(L, S, RAR, analyzer))

            nb_basic_rules += len(RAR)
            nb_rules += nb_consequent

            print('  - L:' + str(L.closure) + ',gen: ' + str(L.generators) + ', nb BR min/max: ' + str(
                len(RAR)) + ', nb CR: ' + str(nb_consequent) + ', TBR: ' + str(nb_basic_rules) + ', TBC: ' + str(
                nb_rules))

            for child in current.children:
                for grandchild in child.children:
                    if grandchild not in to_extract and grandchild not in visited:
                        to_extract.append(grandchild)
            '''

            for rule in RAR:
                text = str(round(rule.support, 2)) + ' - ' + str(
                    round(rule.confidence, 2)) + ': '
                for l in rule.left:
                    text += card_loader.hash_id_name[l] + ' + '
                text += ' ----> '
                for r in rule.right:
                    text += card_loader.hash_id_name[r] + ' + '
                print(text)

    print('nb rules: ' + str(nb_rules))