Пример #1
0
    def test_base_RuleLearner(self):
        """
        Base rule induction learner test. To pass the test, all base
        components are checked, including preprocessors, top-level
        control procedure elements (covering algorithm, rule stopping,
        data stopping), and bottom-level search procedure controller
        (rule finder).

        Every learner that extends _RuleLearner should override the fit
        method. It should at this point not yet be available (exception
        raised).
        """
        base_rule_learner = _RuleLearner()
        self.assertRaises(NotImplementedError, base_rule_learner.fit,
                          self.iris.X, self.iris.Y)

        # test the number of default preprocessors
        self.assertEqual(len(list(base_rule_learner.active_preprocessors)), 2)

        # preprocessor types
        preprocessor_types = [
            type(x) for x in base_rule_learner.active_preprocessors
        ]
        self.assertIn(RemoveNaNClasses, preprocessor_types)
        self.assertIn(Impute, preprocessor_types)

        # test find_rules
        base_rule_learner.domain = self.iris.domain
        base_rule_learner.find_rules(self.iris.X, self.iris.Y.astype(int),
                                     None, None, [], self.iris.domain)

        # test top-level control procedure elements
        self.assertTrue(hasattr(base_rule_learner, "data_stopping"))
        self.assertTrue(hasattr(base_rule_learner, "cover_and_remove"))
        self.assertTrue(hasattr(base_rule_learner, "rule_stopping"))

        # test exclusive covering algorithm
        new_rule = Rule()
        new_rule.covered_examples = np.array([True, False, True], dtype=bool)
        new_rule.target_class = None

        X, Y, W = base_rule_learner.exclusive_cover_and_remove(
            self.iris.X[:3], self.iris.Y[:3], None, new_rule)
        self.assertTrue(len(X) == len(Y) == 1)

        # test rule finder
        self.assertTrue(hasattr(base_rule_learner, "rule_finder"))
        rule_finder = base_rule_learner.rule_finder
        self.assertIsInstance(rule_finder, RuleHunter)
        self.assertTrue(hasattr(rule_finder, "search_algorithm"))
        self.assertTrue(hasattr(rule_finder, "search_strategy"))
        self.assertTrue(hasattr(rule_finder, "quality_evaluator"))
        self.assertTrue(hasattr(rule_finder, "complexity_evaluator"))
        self.assertTrue(hasattr(rule_finder, "general_validator"))
        self.assertTrue(hasattr(rule_finder, "significance_validator"))
Пример #2
0
    def test_base_RuleLearner(self):
        """
        Base rule induction learner test. To pass the test, all base
        components are checked, including preprocessors, top-level
        control procedure elements (covering algorithm, rule stopping,
        data stopping), and bottom-level search procedure controller
        (rule finder).

        Every learner that extends _RuleLearner should override the fit
        method. It should at this point not yet be available (exception
        raised).
        """
        base_rule_learner = _RuleLearner()
        self.assertRaises(NotImplementedError, base_rule_learner.fit,
                          self.iris.X, self.iris.Y)

        # test the number of default preprocessors
        self.assertEqual(len(base_rule_learner.preprocessors), 2)

        # preprocessor types
        preprocessor_types = [type(x) for x in base_rule_learner.preprocessors]
        self.assertIn(RemoveNaNClasses, preprocessor_types)
        self.assertIn(Impute, preprocessor_types)

        # test find_rules
        base_rule_learner.domain = self.iris.domain
        base_rule_learner.find_rules(self.iris.X, self.iris.Y.astype(int),
                                     None, None, [], self.iris.domain)

        # test top-level control procedure elements
        self.assertTrue(hasattr(base_rule_learner, "data_stopping"))
        self.assertTrue(hasattr(base_rule_learner, "cover_and_remove"))
        self.assertTrue(hasattr(base_rule_learner, "rule_stopping"))

        # test exclusive covering algorithm
        new_rule = Rule()
        new_rule.covered_examples = np.array([True, False, True], dtype=bool)
        new_rule.target_class = None

        X, Y, W = base_rule_learner.exclusive_cover_and_remove(
            self.iris.X[:3], self.iris.Y[:3], None, new_rule)
        self.assertTrue(len(X) == len(Y) == 1)

        # test rule finder
        self.assertTrue(hasattr(base_rule_learner, "rule_finder"))
        rule_finder = base_rule_learner.rule_finder
        self.assertIsInstance(rule_finder, RuleHunter)
        self.assertTrue(hasattr(rule_finder, "search_algorithm"))
        self.assertTrue(hasattr(rule_finder, "search_strategy"))
        self.assertTrue(hasattr(rule_finder, "quality_evaluator"))
        self.assertTrue(hasattr(rule_finder, "complexity_evaluator"))
        self.assertTrue(hasattr(rule_finder, "general_validator"))
        self.assertTrue(hasattr(rule_finder, "significance_validator"))