def test_convert(self): X = numpy.arange(8).astype(numpy.float64).reshape((-1, 2)) y = ((X[:, 0] + X[:, 1] * 2) > 10).astype(numpy.int64) y2 = y.copy() y2[0] = 2 tree = DecisionTreeClassifier(max_depth=2) tree.fit(X, y2) self.assertRaise( lambda: NeuralTreeNet.create_from_tree(tree), RuntimeError) tree = DecisionTreeClassifier(max_depth=2) tree.fit(X, y) root = NeuralTreeNet.create_from_tree(tree, 10) self.assertNotEmpty(root) exp = tree.predict_proba(X) got = root.predict(X) self.assertEqual(exp.shape[0], got.shape[0]) self.assertEqualArray(exp, got[:, -2:])
def test_neural_net_gradient(self): X = numpy.arange(8).astype(numpy.float64).reshape((-1, 2)) y = ((X[:, 0] + X[:, 1] * 2) > 10).astype(numpy.int64) ny = label_class_to_softmax_output(y) tree = DecisionTreeClassifier(max_depth=2) tree.fit(X, y) root = NeuralTreeNet.create_from_tree(tree, 10) _, out, err = self.capture(lambda: root.fit(X, ny, verbose=True)) self.assertIn("loss:", out) self.assertEmpty(err)
def test_convert_compact(self): X = numpy.arange(8).astype(numpy.float64).reshape((-1, 2)) y = ((X[:, 0] + X[:, 1] * 2) > 10).astype(numpy.int64) y2 = y.copy() y2[0] = 2 tree = DecisionTreeClassifier(max_depth=2) tree.fit(X, y2) self.assertRaise( lambda: NeuralTreeNet.create_from_tree(tree, arch="k"), ValueError) self.assertRaise( lambda: NeuralTreeNet.create_from_tree(tree, arch="compact"), RuntimeError) tree = DecisionTreeClassifier(max_depth=2) tree.fit(X, y) root = NeuralTreeNet.create_from_tree(tree, 10, arch='compact') self.assertNotEmpty(root) exp = tree.predict_proba(X) got = root.predict(X) self.assertEqual(exp.shape[0], got.shape[0]) self.assertEqualArray(exp + 1e-8, got[:, -2:] + 1e-8) dot = root.to_dot() self.assertIn("s3a4:f4 -> s5a6:f6", dot)
def test_training_weights(self): X = numpy.arange(8).astype(numpy.float64).reshape((-1, 2)) y = ((X[:, 0] + X[:, 1] * 2) > 10).astype(numpy.int64) y2 = y.copy() y2[0] = 2 tree = DecisionTreeClassifier(max_depth=2) tree.fit(X, y) root = NeuralTreeNet.create_from_tree(tree, 10) v1 = root.predict(X[:1]) w = root.training_weights self.assertEqual(w.shape, (11, )) delta = numpy.arange(11) + 0.5 root.update_training_weights(delta) v2 = root.predict(X[:1]) self.assertNotEqualArray(v1, v2)
def test_neural_net_gradient_fit(self): X = numpy.arange(16).astype(numpy.float64).reshape((-1, 2)) y = ((X[:, 0] + X[:, 1] * 2) > 15).astype(numpy.int64) ny = label_class_to_softmax_output(y) tree = DecisionTreeClassifier(max_depth=2) tree.fit(X, y) root = NeuralTreeNet.create_from_tree(tree, 10) loss1 = root.loss(X, ny).sum() self.assertGreater(loss1, -1e-5) self.assertLess(loss1, 1.) _, out, err = self.capture( lambda: root.fit(X, ny, verbose=True, max_iter=20)) self.assertEmpty(err) self.assertNotEmpty(out) loss2 = root.loss(X, ny).sum() self.assertLess(loss2, loss1 + 1)
def test_convert_compact_fit(self): X = numpy.arange(8).astype(numpy.float64).reshape((-1, 2)) y = ((X[:, 0] + X[:, 1] * 2) > 10).astype(numpy.int64) y2 = y.copy() y2[0] = 2 tree = DecisionTreeClassifier(max_depth=2) tree.fit(X, y) root = NeuralTreeNet.create_from_tree(tree, 10, arch='compact') self.assertNotEmpty(root) exp = tree.predict_proba(X) got = root.predict(X) self.assertEqual(exp.shape[0], got.shape[0]) self.assertEqualArray(exp + 1e-8, got[:, -2:] + 1e-8) ny = label_class_to_softmax_output(y) loss1 = root.loss(X, ny).sum() _, out, err = self.capture( lambda: root.fit(X, ny, verbose=True, max_iter=20)) self.assertEmpty(err) self.assertNotEmpty(out) loss2 = root.loss(X, ny).sum() self.assertLess(loss2, loss1 + 1)
def test_dot(self): data = load_iris() X, y = data.data, data.target y = y % 2 tree = DecisionTreeClassifier(max_depth=3, random_state=11) tree.fit(X, y) root = NeuralTreeNet.create_from_tree(tree) dot = export_graphviz(tree) self.assertIn("digraph", dot) dot2 = root.to_dot() self.assertIn("digraph", dot2) x = X[:1].copy() x[0, 3] = 1. dot2 = root.to_dot(X=x.ravel()) self.assertIn("digraph", dot2) exp = tree.predict_proba(X)[:, -1] got = root.predict(X)[:, -1] mat = numpy.empty((exp.shape[0], 2), dtype=exp.dtype) mat[:, 0] = exp mat[:, 1] = got c = numpy.corrcoef(mat.T) self.assertGreater(c[0, 1], 0.5)