def test_null_nodes(self): a = DiscreteVariable("d4", "ab") y = ContinuousVariable("ey") domain = Domain([a], y) data = Table(domain) values = np.array([[42., 43], [44, 45]]) root = DiscreteNode(a, 0, values[1]) root.children = [Node(None, -1, values[0]), None] model = TreeModel(data, root) x = np.array([[0.], [1]]) np.testing.assert_equal(model.get_values(x), values) np.testing.assert_equal(model.get_values_in_python(x), values) np.testing.assert_equal(model.get_values_by_nodes(x), values)
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)
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)