class GiniMetricTest(TestCase): def setUp(self): self.criterion = GiniCriterion() def tearDown(self): pass def test_empty_list(self): x = [] expected_value = 0 result = self.criterion.impurity(x) assert expected_value == result def test_all_same_class(self): x = [0 for _ in range(5)] expected_value = 0 result = self.criterion.impurity(x) assert expected_value == result def test_all_same_class_2(self): x = [2 for _ in range(10)] expected_value = 0 result = self.criterion.impurity(x) assert expected_value == result def test_two_different_class(self): x = [0 if n % 2 == 0 else 1 for n in range(6)] expected_value = 0.5 result = self.criterion.impurity(x) assert expected_value == result def test_three_different_class(self): x = [0, 0, 0, 1, 1, 1, 2, 2, 2] expected_value = 0.66667 result = self.criterion.impurity(x) self.assertAlmostEqual(expected_value, result, 4)
def test_split_chooser_admissible_value(self): self.tree_builder = TreeBuilder(split_criterion=GiniCriterion(), split_chooser=BestSplitChooser(), feature_selection=AllFeatureSelection()) self.assertIsInstance(self.tree_builder.split_chooser, BestSplitChooser)
def test_split_chooser_exception_none_value(self): with self.assertRaises(ValueError): self.tree_builder = TreeBuilder(split_criterion=GiniCriterion(), feature_selection=AllFeatureSelection(), split_chooser=None)
def test_min_gain_split_non_negative_value(self): self.tree_builder = TreeBuilder(split_criterion=GiniCriterion(), split_chooser=BestSplitChooser(), feature_selection=AllFeatureSelection(), min_gain_split=1) self.assertEqual(self.tree_builder.min_gain_split, 1)
def test_min_gain_split_exception_negative_value(self): with self.assertRaises(ValueError): self.tree_builder = TreeBuilder(split_criterion=GiniCriterion(), split_chooser=BestSplitChooser(), feature_selection=AllFeatureSelection(), min_gain_split=-1)
def test_min_samples_split_positive_value_greater_than_one(self): self.tree_builder = TreeBuilder(split_criterion=GiniCriterion(), split_chooser=BestSplitChooser(), feature_selection=AllFeatureSelection(), min_samples_split=2) self.assertEqual(self.tree_builder.min_samples_split, 2)
def test_min_samples_split_exception_less_than_two_instances(self): with self.assertRaises(ValueError): self.tree_builder = TreeBuilder(split_criterion=GiniCriterion(), split_chooser=BestSplitChooser(), feature_selection=AllFeatureSelection(), min_samples_split=1)
def test_min_samples_leaf_positive_value(self): self.tree_builder = TreeBuilder(split_criterion=GiniCriterion(), split_chooser=BestSplitChooser(), feature_selection=AllFeatureSelection(), min_samples_leaf=1) self.assertEqual(self.tree_builder.min_samples_leaf, 1)
def test_min_samples_leaf_exception_none_value(self): with self.assertRaises(ValueError): self.tree_builder = TreeBuilder(split_criterion=GiniCriterion(), split_chooser=BestSplitChooser(), feature_selection=AllFeatureSelection(), min_samples_leaf=None)
def test_max_depth_positive_value(self): self.tree_builder = TreeBuilder(split_criterion=GiniCriterion(), split_chooser=BestSplitChooser(), feature_selection=AllFeatureSelection(), max_depth=1) self.assertEqual(self.tree_builder.max_depth, 1)
def test_max_depth_none_value(self): self.tree_builder = TreeBuilder(split_criterion=GiniCriterion(), split_chooser=BestSplitChooser(), feature_selection=AllFeatureSelection(), max_depth=None) self.assertIsNone(self.tree_builder.max_depth)
def setUp(self): self.tree_builder = TreeBuilder(split_criterion=GiniCriterion(), split_chooser=BestSplitChooser(), feature_selection=AllFeatureSelection())
def setUp(self): self.criterion = GiniCriterion()