Ejemplo n.º 1
0
    def apply(self):
        common_args = dict()
        common_args["n_estimators"] = self.n_estimators
        if self.use_max_features:
            common_args["max_features"] = self.max_features
        if self.use_random_state:
            common_args["random_state"] = self.random_state
        if self.use_max_depth:
            common_args["max_depth"] = self.max_depth
        if self.use_max_leaf_nodes:
            common_args["max_leaf_nodes"] = self.max_leaf_nodes

        learner = RandomForestLearner(
            preprocessors=self.preprocessors,**common_args
        )
        learner.name = self.learner_name
        classifier = None

        if self.data is not None:
            self.error(0)
            if not learner.check_learner_adequacy(self.data.domain):
                self.error(0, learner.learner_adequacy_err_msg)
            else:
                classifier = learner(self.data)
                classifier.name = self.learner_name

        self.send("Learner", learner)
        self.send("Classifier", classifier)
Ejemplo n.º 2
0
    def test_forest_tree_table(self):
        titanic_data = Table('titanic')[::50]
        titanic = RandomForestLearner(n_estimators=3)(titanic_data)
        titanic.instances = titanic_data

        housing_data = Table('housing')[:10]
        housing = RandomForestRegressionLearner(n_estimators=3)(housing_data)
        housing.instances = housing_data

        forest_w = self.create_widget(OWPythagoreanForest)
        for data in (housing, titanic):
            self.send_signal(forest_w.Inputs.random_forest,
                             data,
                             widget=forest_w)
            tree = forest_w.forest_model[0].model

            tree_w = self.widget
            self.send_signal(tree_w.Inputs.tree, tree, widget=tree_w)
            square = [
                i for i in tree_w.scene.items()
                if isinstance(i, SquareGraphicsItem)
            ][-1]
            square.setSelected(True)
            tab = self.get_output(tree_w.Outputs.selected_data, widget=tree_w)
            self.assertGreater(len(tab), 0)
Ejemplo n.º 3
0
    def apply(self):
        common_args = dict()
        common_args["n_estimators"] = self.n_estimators
        if self.use_max_features:
            common_args["max_features"] = self.max_features
        if self.use_random_state:
            common_args["random_state"] = self.random_state
        if self.use_max_depth:
            common_args["max_depth"] = self.max_depth
        if self.use_max_leaf_nodes:
            common_args["max_leaf_nodes"] = self.max_leaf_nodes

        learner = RandomForestLearner(
            preprocessors=self.preprocessors,**common_args
        )
        learner.name = self.learner_name
        classifier = None

        if self.data is not None:
            self.error(0)
            if not learner.check_learner_adequacy(self.data.domain):
                self.error(0, learner.learner_adequacy_err_msg)
            else:
                classifier = learner(self.data)
                classifier.name = self.learner_name

        self.send("Learner", learner)
        self.send("Classifier", classifier)
Ejemplo n.º 4
0
    def test_changing_data_restores_depth_from_previous_settings(self):
        titanic_data = Table("titanic")[::50]
        forest = RandomForestLearner(n_estimators=3)(titanic_data)
        forest.instances = titanic_data

        self.send_signal(self.widget.Inputs.tree, forest.trees[0])
        self.widget.controls.depth_limit.setValue(1)

        # The domain is still the same, so restore the depth limit from before
        self.send_signal(self.widget.Inputs.tree, forest.trees[1])
        self.assertEqual(self.widget.ptree._depth_limit, 1)
Ejemplo n.º 5
0
    def test_context(self):
        iris = Table("iris")
        iris_tree = RandomForestLearner()(iris)
        iris_tree.instances = iris
        self.send_signal(self.widget.Inputs.random_forest, self.titanic)
        self.widget.target_class_index = 1

        self.send_signal(self.widget.Inputs.random_forest, iris_tree)
        self.assertEqual(0, self.widget.target_class_index)

        self.widget.target_class_index = 2
        self.send_signal(self.widget.Inputs.random_forest, self.titanic)
        self.assertEqual(1, self.widget.target_class_index)

        self.send_signal(self.widget.Inputs.random_forest, iris_tree)
        self.assertEqual(2, self.widget.target_class_index)
Ejemplo n.º 6
0
    def test_forest_tree_table(self):
        titanic_data = Table('titanic')[::50]
        titanic = RandomForestLearner(n_estimators=3)(titanic_data)
        titanic.instances = titanic_data

        housing_data = Table('housing')[:10]
        housing = RandomForestRegressionLearner(n_estimators=3)(housing_data)
        housing.instances = housing_data

        forest_w = self.create_widget(OWPythagoreanForest)
        for data in (housing, titanic):
            self.send_signal(forest_w.Inputs.random_forest, data, widget=forest_w)
            tree = forest_w.forest_model[0].model

            tree_w = self.widget
            self.send_signal(tree_w.Inputs.tree, tree, widget=tree_w)
            square = [i for i in tree_w.scene.items() if isinstance(i, SquareGraphicsItem)][-1]
            square.setSelected(True)
            tab = self.get_output(tree_w.Outputs.selected_data, widget=tree_w)
            self.assertGreater(len(tab), 0)
Ejemplo n.º 7
0
    def setUpClass(cls):
        super().setUpClass()

        # Set up for widget tests
        titanic_data = Table('titanic')[::50]
        cls.titanic = RandomForestLearner(n_estimators=3)(titanic_data)
        cls.titanic.instances = titanic_data

        housing_data = Table('housing')[:10]
        cls.housing = RandomForestRegressionLearner(
            n_estimators=3)(housing_data)
        cls.housing.instances = housing_data
Ejemplo n.º 8
0
        return self._adapters

    @property
    def domain(self):
        """Get the domain."""
        return self._domain


if __name__ == '__main__':
    from AnyQt.QtWidgets import QApplication
    import sys

    app = QApplication(sys.argv)
    ow = OWPythagoreanForest()
    data = Table(sys.argv[1] if len(sys.argv) > 1 else 'iris')

    if data.domain.has_discrete_class:
        from Orange.classification.random_forest import \
            RandomForestLearner as RFLearner
    else:
        from Orange.regression.random_forest import \
            RandomForestRegressionLearner as RFLearner
    rf = RFLearner(n_estimators=10, max_depth=1000)(data)
    rf.instances = data
    ow.set_rf(rf)

    ow.show()
    ow.raise_()
    ow.handleNewSignals()
    app.exec_()
Ejemplo n.º 9
0
        return self._adapters

    @property
    def domain(self):
        """Get the domain."""
        return self._domain


if __name__ == '__main__':
    from AnyQt.QtWidgets import QApplication
    import sys

    app = QApplication(sys.argv)
    ow = OWPythagoreanForest()
    data = Table(sys.argv[1] if len(sys.argv) > 1 else 'iris')

    if data.domain.has_discrete_class:
        from Orange.classification.random_forest import \
            RandomForestLearner as RFLearner
    else:
        from Orange.regression.random_forest import \
            RandomForestRegressionLearner as RFLearner
    rf = RFLearner(n_estimators=10, max_depth=1000)(data)
    rf.instances = data
    ow.set_rf(rf)

    ow.show()
    ow.raise_()
    ow.handleNewSignals()
    app.exec_()
Ejemplo n.º 10
0
        ]
        return self._adapters

    @property
    def domain(self):
        """Get the domain."""
        return self._domain


if __name__ == '__main__':
    from AnyQt.QtWidgets import QApplication
    import sys

    app = QApplication(sys.argv)
    data = Table(sys.argv[1] if len(sys.argv) > 1 else 'iris')

    if data.domain.has_discrete_class:
        from Orange.classification.random_forest import RandomForestLearner
    else:
        from Orange.regression.random_forest import \
            RandomForestRegressionLearner as RandomForestLearner
    rf = RandomForestLearner()(data)
    rf.instances = data

    ow = OWPythagoreanForest()
    ow.set_rf(rf)

    ow.show()
    ow.handleNewSignals()
    app.exec_()