From e1560765392121ec3bddbb17f46125ac9b858e56 Mon Sep 17 00:00:00 2001 From: Marcin Michalski Date: Wed, 7 Mar 2018 23:16:25 +0100 Subject: [PATCH] Add DecisionTreeLeaf.getNodeImpurityDecrease test (#261) --- .../DecisionTree/DecisionTreeLeafTest.php | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/Classification/DecisionTree/DecisionTreeLeafTest.php b/tests/Classification/DecisionTree/DecisionTreeLeafTest.php index abcf10d..e694878 100644 --- a/tests/Classification/DecisionTree/DecisionTreeLeafTest.php +++ b/tests/Classification/DecisionTree/DecisionTreeLeafTest.php @@ -23,4 +23,28 @@ class DecisionTreeLeafTest extends TestCase $this->assertEquals('
col_0 =1
Gini: 0.00
 No |
col_1 <= 2
Gini: 0.00
', $leaf->getHTML()); } + + public function testNodeImpurityDecreaseShouldBeZeroWhenLeafIsTerminal(): void + { + $leaf = new DecisionTreeLeaf(); + $leaf->isTerminal = true; + + $this->assertEquals(0.0, $leaf->getNodeImpurityDecrease(1)); + } + + public function testNodeImpurityDecrease(): void + { + $leaf = new DecisionTreeLeaf(); + $leaf->giniIndex = 0.5; + $leaf->records = [1, 2, 3]; + + $leaf->leftLeaf = new DecisionTreeLeaf(); + $leaf->leftLeaf->records = [5, 2]; + + $leaf->rightLeaf = new DecisionTreeLeaf(); + $leaf->rightLeaf->records = []; + $leaf->rightLeaf->giniIndex = 0.3; + + $this->assertSame(0.75, $leaf->getNodeImpurityDecrease(2)); + } }