Exemplo n.º 1
0
def process(infile, algorithm_name, support, confidence, m, random, partial):
    stats = Stats()
    transactions = TransactionsList(infile)
    stats.record_post_large_sets()
    stats.record_post_rules()
    last_total_time = stats.real_time
    last_user_time = stats.user_time
    stats = Stats()
    if algorithm_name == 'apriori':
        algorithm = Apriori(transactions, support)
    else:
        algorithm = Dic(transactions, support, m, random, partial)
    large_sets, counter = algorithm.get_large_sets_and_counter()
    stats.record_post_large_sets()
    rules = RulesGenerator.generate_rules(large_sets, confidence, counter, transactions)
    stats.record_post_rules()
    large_len = len(large_sets)
    total_time = stats.real_time - last_total_time
    user_time = stats.user_time - last_user_time
    large_sets_time = stats.set_gen_time - last_total_time
    last_total_time = stats.real_time
    last_user_time = stats.user_time
    memory = stats.memory_use
    rules_no = len(rules)

    print "{infile}\t{algorithm_name}\t{support}\t{confidence}\t{m}\t{rules_no}\t{large_len}\t{memory}\t{total_time}\t{user_time}\t{large_sets_time}\t{partial}\t{random}".format(**locals())
Exemplo n.º 2
0
 def testRules(self):
     extract = lambda x: (x[0], tuple(x[1]))
     result = RulesGenerator.generate_rules(self.large_sets, self.minconf, self.counter, self.transactions)
     result = [ extract(x) for x in result ]
     expected = [
             ((2,), (3,)), #conf=0.75
             ((3,), (2,)), #conf=1.0
             ((4,), (2,)), #conf=1.0
             ((4,), (3,)), #conf=1.0
             #((4,), (2, 3)), #conf=0.5
             #((2, 4), (3,)), #conf=0.5
             #((3, 4), (2,)), #conf=0.5
         ]
     self.assertEqual(result, expected)
Exemplo n.º 3
0
 def testRules(self):
     extract = lambda x: (x[0], tuple(x[1]))
     result = RulesGenerator.generate_rules(self.large_sets, self.minconf,
                                            self.counter, self.transactions)
     result = [extract(x) for x in result]
     expected = [
         ((2, ), (3, )),  #conf=0.75
         ((3, ), (2, )),  #conf=1.0
         ((4, ), (2, )),  #conf=1.0
         ((4, ), (3, )),  #conf=1.0
         #((4,), (2, 3)), #conf=0.5
         #((2, 4), (3,)), #conf=0.5
         #((3, 4), (2,)), #conf=0.5
     ]
     self.assertEqual(result, expected)
Exemplo n.º 4
0
def main(args):
    stats = Stats()
    transactions = TransactionsList(args.infile)
    if args.algorithm == 'apriori':
        algorithm = Apriori(transactions, args.minsup)
    else:
        algorithm = Dic(transactions, args.minsup, args.m)
    large_sets, counter = algorithm.get_large_sets_and_counter()
    stats.record_post_large_sets()
    rules = RulesGenerator.generate_rules(large_sets, args.minconf, counter, transactions)
    stats.record_post_rules()

    writer = Writer(args.outfile)
    writer.add_args(args)
    writer.add_stats(stats)
    writer.add_rules(rules)
    writer.write()
Exemplo n.º 5
0
 def testGetAllSubsets(self):
     result = RulesGenerator._RulesGenerator__get_all_subsets(self.set)
     expected = [(1, ), (2, ), (3, ), (1, 2), (2, 3), (1, 3)]
     self.assertEqual(sorted(result), sorted(expected))
Exemplo n.º 6
0
 def testGetAllSubsets(self):
     result = RulesGenerator._RulesGenerator__get_all_subsets(self.set)
     expected = [(1,), (2,), (3,), (1, 2), (2, 3), (1, 3)]
     self.assertEqual(sorted(result), sorted(expected))