for key in self.support: print(list(key), f"{self.support[key]:.2f}") print() if __name__ == "__main__": # Init database and field list db = Database(['A', 'B', 'C', 'D', 'E', 'F']) # Create transactions t1 = Transaction(0, frozenset(['A', 'B', 'C'])) t2 = Transaction(1, frozenset(['A', 'C'])) t3 = Transaction(2, frozenset(['A', 'D'])) t4 = Transaction(3, frozenset(['B', 'E', 'F'])) # Add transactions to database db.add_transaction(t1) db.add_transaction(t2) db.add_transaction(t3) db.add_transaction(t4) # Output database db.print_as_set() db.print_as_boolean() # Init and run Apriory alg = AprioriAlgorithm(db, 0.30, 0.50) alg.run() # Print results print('Support of frequent itemsets:') alg.print_support()
'''Print support of frequent itemsets''' itemsets = list(self.support.keys()) itemsets.sort(key=lambda item: self.support[item], reverse=True) for item in itemsets: print(f"{list(item)} supp = {self.support[item]:.2f}") print() if __name__ == "__main__": # initialize database object with list of fields db = Database(['A', 'B', 'C', 'D', 'E', 'F', 'G']) # add transactions to database db.add_transaction(Transaction(0, ['A', 'D', 'F'])) db.add_transaction(Transaction(1, ['A', 'C', 'D', 'E'])) db.add_transaction(Transaction(2, ['B', 'D'])) db.add_transaction(Transaction(3, ['B', 'C', 'D'])) db.add_transaction(Transaction(4, ['B', 'C'])) db.add_transaction(Transaction(5, ['A', 'B', 'D'])) db.add_transaction(Transaction(6, ['B', 'D', 'E'])) db.add_transaction(Transaction(7, ['B', 'C', 'E', 'G'])) db.add_transaction(Transaction(8, ['C', 'D', 'F'])) db.add_transaction(Transaction(9, ['A', 'B', 'D'])) # print database db.print_as_boolean() # create and build fp-tree fpt = FPTree(db)