File: test_simple_tree.py

package info (click to toggle)
orange3 3.40.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,908 kB
  • sloc: python: 162,745; ansic: 622; makefile: 322; sh: 93; cpp: 77
file content (55 lines) | stat: -rw-r--r-- 1,894 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import unittest

import numpy as np

from Orange.classification import SimpleTreeLearner
from Orange.data import ContinuousVariable, DiscreteVariable, Domain, \
    Table


class SimpleTreeTest(unittest.TestCase):
    def test_nonan_classification(self):
        x = ContinuousVariable("x")
        y = DiscreteVariable("y", values=tuple("ab"))
        d = Domain([x], y)
        t = Table.from_numpy(d, [[0]], [np.nan])
        m = SimpleTreeLearner()(t)
        self.assertFalse(np.isnan(m(t)[0]))

    def test_nonan_regression(self):
        x = ContinuousVariable("x")
        y = ContinuousVariable("y")
        d = Domain([x], y)
        t = Table.from_numpy(d, [[42]], [np.nan])
        m = SimpleTreeLearner()(t)
        # must not be nan ...
        self.assertFalse(np.isnan(m(t)[0]))
        # ... and currently, it's zero (although mathematicians disagree,
        # we, engineers *know* that 0 is the mean of R)
        self.assertEqual(m(t)[0], 0)

        x2 = ContinuousVariable("x2")
        d = Domain([x, x2], y)
        t = Table.from_numpy(d,
                             [[-1, np.nan], [1, -1], [1, 1]],
                             [-20, 20, np.nan])
        m = SimpleTreeLearner(min_instances=1)(t)
        # must not be nan ...
        self.assertFalse(np.isnan(m(t)[0]))
        # ... and currently, it's zero (although mathematicians disagree,
        # we, engineers *know* that 0 is the mean of R)
        np.testing.assert_equal(m(t), [-20, 20, 20])

    def test_stub(self):
        x = ContinuousVariable("x")
        y = ContinuousVariable("y")
        d = Domain([x], y)
        t = Table.from_numpy(d, [[-1], [1]], [-5, 0])
        m = SimpleTreeLearner(min_instances=1)(t)
        np.testing.assert_equal(m(t), [-5, 0])
        m = SimpleTreeLearner()(t)
        np.testing.assert_equal(m(t), [-2.5, -2.5])


if __name__ == "__main__":
    unittest.main()