def test_compile_and_run_cont(self):
        # I investigate, I have a warrant
        # pylint: disable=protected-access
        model = TreeModel(self.data, self.root)
        expected_values = np.vstack((np.arange(8), [42] * 8)).T
        np.testing.assert_equal(model._values, expected_values)
        self.assertEqual(model._thresholds[0], 13)
        self.assertEqual(model._thresholds.shape, (8, ))

        nan = float("nan")
        x = np.array(
            [
                [nan, 0, 0],
                [13, nan, 0],
                [13, 0, 0],
                [13, 1, 0],
                [13, 2, 0],
                [14, 2, nan],
                [14, 2, 2],
                [14, 2, 1],
            ],
            dtype=float,
        )
        np.testing.assert_equal(model.get_values(x), expected_values)
        np.testing.assert_equal(model.get_values_in_python(x), expected_values)
        np.testing.assert_equal(model.get_values_by_nodes(x), expected_values)
        np.testing.assert_equal(model.predict(x), np.arange(8).astype(int))

        v1 = ContinuousVariable("d1")
        v2 = DiscreteVariable("d2", "abc")
        v3 = DiscreteVariable("d3", "def")
        y = DiscreteVariable("dy")
        domain = Domain([v1, v2, v3], y)
        data = Table(domain, np.zeros((10, 4)))
        root = NumericNode(v1, 0, 13, np.array([0.0, 42]))
        left = DiscreteNode(v2, 1, np.array([1, 42]))
        left.children = [
            Node(None, None, np.array([x, 42])) for x in [2, 3, 4]
        ]
        right = MappedDiscreteNode(v3, 2, np.array([1, 1, 0]),
                                   np.array([5, 42]))
        right.children = [Node(None, None, np.array([x, 42])) for x in [6, 7]]
        root.children = [left, right]

        model = TreeModel(data, root)
        normalized = expected_values / np.sum(expected_values,
                                              axis=1)[:, np.newaxis]
        np.testing.assert_equal(model.predict(x), normalized)
Exemplo n.º 2
0
    def test_compile_and_run_cont(self):
        # I investigate, I have a warrant
        # pylint: disable=protected-access
        model = TreeModel(self.data, self.root)
        expected_values = np.vstack((np.arange(8), [42] * 8)).T
        np.testing.assert_equal(model._values, expected_values)
        self.assertEqual(model._thresholds[0], 13)
        self.assertEqual(model._thresholds.shape, (8,))

        nan = float("nan")
        x = np.array(
            [[nan, 0, 0],
             [13, nan, 0],
             [13, 0, 0],
             [13, 1, 0],
             [13, 2, 0],
             [14, 2, nan],
             [14, 2, 2],
             [14, 2, 1]], dtype=float
        )
        np.testing.assert_equal(model.get_values(x), expected_values)
        np.testing.assert_equal(model.get_values_in_python(x), expected_values)
        np.testing.assert_equal(model.get_values_by_nodes(x), expected_values)
        np.testing.assert_equal(model.predict(x), np.arange(8).astype(int))

        v1 = ContinuousVariable("d1")
        v2 = DiscreteVariable("d2", "abc")
        v3 = DiscreteVariable("d3", "def")
        y = DiscreteVariable("dy")
        domain = Domain([v1, v2, v3], y)
        data = Table(domain, np.zeros((10, 4)))
        root = NumericNode(v1, 0, 13, np.array([0., 42]))
        left = DiscreteNode(v2, 1, np.array([1, 42]))
        left.children = [Node(None, None, np.array([x, 42])) for x in [2, 3, 4]]
        right = MappedDiscreteNode(v3, 2, np.array([1, 1, 0]),
                                   np.array([5, 42]))
        right.children = [Node(None, None, np.array([x, 42])) for x in [6, 7]]
        root.children = [left, right]

        model = TreeModel(data, root)
        normalized = \
            expected_values / np.sum(expected_values, axis=1)[:, np.newaxis]
        np.testing.assert_equal(model.predict(x), normalized)
Exemplo n.º 3
0
 def setUp(self):
     """
     Construct a tree with v1 as a root, and v2 and v3 as left and right
     child.
     """
     # pylint: disable=invalid-name
     v1 = self.v1 = ContinuousVariable("v1")
     v2 = self.v2 = DiscreteVariable("v2", "abc")
     v3 = self.v3 = DiscreteVariable("v3", "def")
     y = self.y = ContinuousVariable("y")
     self.domain = Domain([v1, v2, v3], y)
     self.data = Table(self.domain, np.arange(40).reshape(10, 4))
     self.root = NumericNode(v1, 0, 13, np.array([0., 42]))
     self.root.subset = np.array([], dtype=np.int32)
     left = DiscreteNode(v2, 1, np.array([1, 42]))
     left.children = [Node(None, None, np.array([x, 42])) for x in [2, 3, 4]]
     right = MappedDiscreteNode(v3, 2, np.array([1, 1, 0]),
                                np.array([5, 42]))
     right.children = [Node(None, None, np.array([x, 42])) for x in [6, 7]]
     self.root.children = [left, right]
 def setUp(self):
     # pylint: disable=invalid-name
     v1 = self.v1 = ContinuousVariable("v1")
     v2 = self.v2 = DiscreteVariable("v2", "abc")
     v3 = self.v3 = DiscreteVariable("v3", "def")
     y = self.y = ContinuousVariable("y")
     self.domain = Domain([v1, v2, v3], y)
     self.data = Table(self.domain, np.arange(40).reshape(10, 4))
     self.root = NumericNode(v1, 0, 13, np.array([0.0, 42]))
     self.root.subset = np.array(np.arange(10), dtype=np.int32)
     left = self.left = DiscreteNode(v2, 1, np.array([1, 42]))
     left.subset = np.array([2, 3, 4, 5])
     left.children = [Node(None, None, np.array([x, 42])) for x in [2, 3, 4]]
     right = self.right = MappedDiscreteNode(
         v3, 2, np.array([1, 1, 0]), np.array([5, 42])
     )
     right.children = [Node(None, None, np.array([6, 42])), None]
     right.subset = np.array([8, 9])
     self.root.children = [left, right]
     self.model = TreeModel(self.data, self.root)
     self.adapter = TreeAdapter(self.model)
Exemplo n.º 5
0
    def test_mapped_node(self):
        var = DiscreteVariable("y", values="abc")
        node = MappedDiscreteNode(var, 2, np.array([1, 1, 0]), "foo")
        self.assertEqual(node.attr, var)
        self.assertEqual(node.attr_idx, 2)
        self.assertEqual(node.value, "foo")
        self.assertEqual(node.children, [])
        np.testing.assert_equal(node.subset, np.array([], dtype=np.int32))

        self.assertEqual(node.descend([3, 4, 0, 6]), 1)
        self.assertEqual(node.descend([3, 4, 1, 6]), 1)
        self.assertEqual(node.descend([3, 4, 2, 6]), 0)
        self.assertTrue(np.isnan(node.descend([3, 4, float("nan"), 6])))

        mapping, branches = MappedDiscreteNode.branches_from_mapping(
            np.array([2, 3, 1, 1, 0, 1, 4, 2]), int("1001", 2), 6)
        np.testing.assert_equal(
            mapping, np.array([1, 0, 0, 1, 0, 0], dtype=np.int16))
        np.testing.assert_equal(
            branches, np.array([0, 1, 0, 0, 1, 0, 0, 0], dtype=np.int16))