source: orange/testing/unittests/tests/test_association.py @ 9019:3847a6ba46e1

Revision 9019:3847a6ba46e1, 4.2 KB checked in by ales_erjavec <ales.erjavec@…>, 3 years ago (diff)

Added unittest for association rules.

Line 
1from __future__ import division
2from Orange import associate
3from Orange.misc import testing
4from Orange.misc.testing import test_on_data, datasets_driven
5import unittest
6import pickle
7
8# helper functions
9
10def test_inducer_on(self, table):
11    rules = self.inducer(table)
12    if self.inducer.store_examples and rules:
13        self.assertIsNotNone(rules[0].examples)
14       
15    self.assertLessEqual(len(rules), self.inducer.max_item_sets)
16    print "\n%5s   %5s" % ("supp", "conf")
17    for r in rules:
18        print "%5.3f   %5.3f   %s" % (r.support, r.confidence, r)
19        self.assertGreaterEqual(r.support, self.inducer.support)
20        self.assertIsNotNone(r.left)
21        self.assertIsNotNone(r.right)
22        self.assertAlmostEqual(r.support, r.n_applies_both / r.n_examples, places=3)
23        self.assertAlmostEqual(r.confidence, r.n_applies_both / r.n_applies_left, places=3)
24        self.assertAlmostEqual(r.coverage, r.n_applies_left / r.n_examples, places=3)
25        self.assertAlmostEqual(r.strength, r.n_applies_right / r.n_applies_left, places=3)
26        self.assertAlmostEqual(r.lift, r.n_examples * r.n_applies_both / (r.n_applies_left * r.n_applies_right), places=3)
27#        self.assertAlmostEqual(r.leverage, (r.n_examples * r.n_applies_both - r.n_applies_left * r.n_applies_right) / 100.0)
28   
29    itemsets = self.inducer.get_itemsets(table)
30    self.rules = rules
31
32
33def test_pickling_on(self, table):
34    rules = self.inducer(table)
35    rules_clone = pickle.loads(pickle.dumps(rules))
36    inducer_clone = pickle.loads(pickle.dumps(self.inducer))
37    rules_clone1 = inducer_clone(table)
38    for r1, r2, r3 in zip(rules, rules_clone, rules_clone1):
39        self.assertEqual(r1.support, r2.support)
40        self.assertEqual(r2.support, r3.support)
41       
42        self.assertEqual(r1.confidence, r2.confidence)
43        self.assertEqual(r2.confidence, r3.confidence)
44       
45        self.assertEqual(r1.coverage, r2.coverage)
46        self.assertEqual(r2.coverage, r3.coverage)
47       
48        self.assertEqual(r1.strength, r2.strength)
49        self.assertEqual(r2.strength, r3.strength)
50       
51        for inst in table:
52            self.assertEqual(r1.applies_left(inst), r2.applies_left(inst))
53            self.assertEqual(r2.applies_left(inst), r3.applies_left(inst))
54           
55            self.assertEqual(r1.applies_right(inst), r2.applies_right(inst))
56            self.assertEqual(r2.applies_right(inst), r3.applies_right(inst))
57           
58            self.assertEqual(r1.applies_both(inst), r2.applies_both(inst))
59            self.assertEqual(r2.applies_both(inst), r3.applies_both(inst))
60           
61           
62@datasets_driven(datasets=["inquisition.basket"])
63class TestSparseInducer(unittest.TestCase):
64    def setUp(self):
65        self.inducer = associate.AssociationRulesSparseInducer(support=0.5,
66                                store_examples=True, max_item_sets=2000)
67       
68    @test_on_data
69    def test_inducer_on(self, table):
70        test_inducer_on(self, table)
71
72
73    @test_on_data
74    def test_pickling_on(self, table):
75        test_pickling_on(self, table)
76               
77
78@datasets_driven(datasets=["lenses", "monks-1"])
79class TestInducer(unittest.TestCase):
80    def setUp(self):
81        self.inducer = associate.AssociationRulesInducer(support=0.2,
82                            confidence=0.5, store_examples=True,
83                            max_item_sets=2000)
84       
85    @test_on_data
86    def test_inducer_on(self, table):
87        test_inducer_on(self, table)
88       
89    @test_on_data
90    def test_pickling_on(self, table):
91        test_pickling_on(self, table)
92       
93       
94@datasets_driven(datasets=["lenses", "monks-1"])
95class TestInducerClassification(unittest.TestCase):
96    def setUp(self):
97        self.inducer = associate.AssociationRulesInducer(support=0.2,
98                            confidence=0.5, store_examples=True,
99                            max_item_sets=2000,
100                            classification_rules=True)
101       
102    @test_on_data
103    def test_inducer_on(self, table):
104        test_inducer_on(self, table)
105       
106    @test_on_data
107    def test_pickling_on(self, table):
108        test_pickling_on(self, table)
109       
110
111if __name__ is "__main__":
112    unittest.main()
113
114   
Note: See TracBrowser for help on using the repository browser.