|
5 | 5 |
|
6 | 6 | import numpy as np |
7 | 7 | import scipy.sparse as sp |
| 8 | +from Orange.classification._tree_scorers import find_threshold_entropy |
8 | 9 |
|
9 | 10 | from Orange.data import Table, Domain, DiscreteVariable, ContinuousVariable |
10 | 11 | from Orange.classification.tree import \ |
@@ -448,3 +449,36 @@ def test_compile_and_run_cont_sparse(self): |
448 | 449 | [14, 2, 1]], dtype=float |
449 | 450 | )) |
450 | 451 | np.testing.assert_equal(model.get_values(x), expected_values) |
| 452 | + |
| 453 | + |
| 454 | +class TestScorers(unittest.TestCase): |
| 455 | + |
| 456 | + def test_find_threshold_entropy(self): |
| 457 | + x = np.array([1, 2, 3, 4], dtype=float) |
| 458 | + y = np.array([0, 0, 1, 1], dtype=float) |
| 459 | + ind = np.argsort(x, kind="stable") |
| 460 | + e, t = find_threshold_entropy(x, y, ind, 2, 1) |
| 461 | + self.assertAlmostEqual(e, 1) |
| 462 | + self.assertEqual(t, 2.0) |
| 463 | + |
| 464 | + def test_find_threshold_entropy_repeated(self): |
| 465 | + x = np.array([1, 1, 1, 2, 2, 2], dtype=float) |
| 466 | + y = np.array([0, 0, 0, 0, 1, 1], dtype=float) |
| 467 | + ind = np.argsort(x, kind="stable") |
| 468 | + e, t = find_threshold_entropy(x, y, ind, 2, 1) |
| 469 | + self.assertAlmostEqual(e, 0.459147917027245) |
| 470 | + self.assertEqual(t, 1.0) |
| 471 | + |
| 472 | + x = np.array([1, 1, 1, 2, 2, 2], dtype=float) |
| 473 | + y = np.array([0, 0, 1, 1, 1, 1], dtype=float) |
| 474 | + ind = np.argsort(x, kind="stable") |
| 475 | + e, t = find_threshold_entropy(x, y, ind, 2, 1) |
| 476 | + self.assertAlmostEqual(e, 0.459147917027245) |
| 477 | + self.assertEqual(t, 1.0) |
| 478 | + |
| 479 | + x = np.array([1, 1, 1, 2, 2, 2], dtype=float) |
| 480 | + y = np.array([0, 1, 1, 1, 1, 1], dtype=float) |
| 481 | + ind = np.argsort(x, kind="stable") |
| 482 | + e, t = find_threshold_entropy(x, y, ind, 2, 1) |
| 483 | + self.assertAlmostEqual(e, 0.19087450462110966) |
| 484 | + self.assertEqual(t, 1.0) |
0 commit comments