Exemplo n.º 1
0
 def test_learner_with_transformation(self):
     learner = RandomForestLearner(random_state=0)
     from Orange.projection import PCA
     iris = Table("iris")
     data = PCA(n_components=2)(iris)(iris)
     scores = learner.score_data(data)
     np.testing.assert_almost_equal(scores, [[0.7760495, 0.2239505]])
Exemplo n.º 2
0
 def test_learner_with_transformation(self):
     learner = RandomForestLearner(random_state=0)
     from Orange.projection import PCA
     iris = Table("iris")
     data = PCA(n_components=2)(iris)(iris)
     scores = learner.score_data(data)
     np.testing.assert_almost_equal(scores, [[0.7760495, 0.2239505]])
Exemplo n.º 3
0
    def test_input_scorer_fitter(self):
        heart_disease = Table('heart_disease')
        self.assertEqual(self.widget.scorers, {})

        model = self.widget.ranksModel

        for fitter, name in ((RandomForestLearner(), 'random forest'),
                             (SGDLearner(), 'sgd')):
            with self.subTest(fitter=fitter),\
                    warnings.catch_warnings():
                warnings.filterwarnings("ignore", ".*", ConvergenceWarning)
                self.send_signal("Scorer", fitter, 1)

                for data in (self.housing, heart_disease):
                    with self.subTest(data=data.name):
                        self.send_signal('Data', data)
                        self.wait_until_finished()
                        scores = [
                            model.data(
                                model.index(row,
                                            model.columnCount() - 1))
                            for row in range(model.rowCount())
                        ]
                        self.assertEqual(len(scores),
                                         len(data.domain.attributes))
                        self.assertFalse(np.isnan(scores).any())

                        last_column = model.headerData(model.columnCount() - 1,
                                                       Qt.Horizontal).lower()
                        self.assertIn(name, last_column)

                self.send_signal("Scorer", None, 1)
                self.assertEqual(self.widget.scorers, {})
Exemplo n.º 4
0
    def test_input_scorer_fitter(self):
        heart_disease = Table("heart_disease")
        self.assertEqual(self.widget.scorers, {})

        model = self.widget.ranksModel

        for fitter, name in (
            (RandomForestLearner(), "random forest"),
            (SGDLearner(), "sgd"),
        ):
            with self.subTest(fitter=fitter):
                self.send_signal("Scorer", fitter, 1)

                for data in (self.housing, heart_disease):
                    with self.subTest(data=data.name):
                        self.send_signal("Data", data)
                        scores = [
                            model.data(
                                model.index(row,
                                            model.columnCount() - 1))
                            for row in range(model.rowCount())
                        ]
                        self.assertEqual(len(scores),
                                         len(data.domain.attributes))
                        self.assertFalse(np.isnan(scores).any())

                        last_column = model.headerData(model.columnCount() - 1,
                                                       Qt.Horizontal).lower()
                        self.assertIn(name, last_column)

                self.send_signal("Scorer", None, 1)
                self.assertEqual(self.widget.scorers, {})
Exemplo n.º 5
0
    def test_no_class_data_learner_class_reg(self):
        """
        Check workflow with learners that can be both classifier
        or regressor and data have no class variable. This test should not
        fail.
        """
        data = Table.from_table(Domain(self.iris.domain.variables), self.iris)
        random_forest = RandomForestLearner()
        self.assertIsNone(data.domain.class_var)
        self.send_signal(self.widget.Inputs.data, data)

        with patch("Orange.widgets.data.owrank.log.error") as log:
            self.send_signal(self.widget.Inputs.scorer, random_forest, 1)
            log.assert_called()

        self.assertEqual(self.get_output(self.widget.Outputs.scores).X.shape,
                         (len(self.iris.domain.variables), 1))
Exemplo n.º 6
0
        self._trees = model.trees

    def get_trees(self):
        """Get the tree adapters in the random forest."""
        if not self._adapters:
            self._adapters = list(map(SklTreeAdapter, self._trees))
        return self._adapters

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


if __name__ == '__main__':
    from Orange.modelling import RandomForestLearner
    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')
    rf = RandomForestLearner(n_estimators=10)(data)
    rf.instances = data
    ow.set_rf(rf)

    ow.show()
    ow.raise_()
    ow.handleNewSignals()
    app.exec_()
Exemplo n.º 7
0
        self._trees = model.trees

    def get_trees(self):
        """Get the tree adapters in the random forest."""
        if not self._adapters:
            self._adapters = list(map(SklTreeAdapter, self._trees))
        return self._adapters

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


if __name__ == '__main__':
    from Orange.modelling import RandomForestLearner
    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')
    rf = RandomForestLearner(n_estimators=10)(data)
    rf.instances = data
    ow.set_rf(rf)

    ow.show()
    ow.raise_()
    ow.handleNewSignals()
    app.exec_()
Exemplo n.º 8
0
class OWCatBoost(OWBaseLearner):
    name = "CatBoost"
    description = "An ensemble meta-algorithm"
    icon = "icons/mywidget.svg"
    category = "Model"

    priority = 80

    LEARNER = CatBoostLearner
    DEFAULT_BASE_ESTIMATOR = RandomForestLearner()

    random_seed = Setting(0)
    n_iterations = Setting(50)
    depth = Setting(6)
    learning_rate = Setting(.03)
    loss_index = Setting(0)
    border = Setting(.5)
    use_random_seed = Setting(False)
    train_dir = Setting(tempfile.gettempdir())
    losses = ["Logloss", "MultiClass"]

    class Error(OWBaseLearner.Error):
        no_weight_support = Msg('The base learner does not support weights.')

    class Inputs(OWBaseLearner.Inputs):
        learner = Input("Learner", Learner)

    def add_main_layout(self):
        box = gui.widgetBox(self.controlArea, "Parameters")
        self.base_estimator = self.DEFAULT_BASE_ESTIMATOR
        self.border_spin = gui.doubleSpin(box,
                                          self,
                                          "border",
                                          0,
                                          1.0,
                                          .1,
                                          label="Border:",
                                          decimals=2,
                                          alignment=Qt.AlignRight,
                                          controlWidth=80,
                                          callback=self.settings_changed)
        self.base_label = gui.label(
            box, self, "Base learner: " + self.base_estimator.name.title()
            if self.base_estimator is not None else 'None')
        self.path_edit = gui.lineEdit(box,
                                      self,
                                      "train_dir",
                                      label="Path for training:",
                                      alignment=Qt.AlignRight,
                                      callback=self.settings_changed)
        self.n_estimators_spin = gui.spin(box,
                                          self,
                                          "n_iterations",
                                          1,
                                          100,
                                          label="Number of estimators:",
                                          alignment=Qt.AlignRight,
                                          controlWidth=80,
                                          callback=self.settings_changed)
        self.depth_spin = gui.spin(box,
                                   self,
                                   "depth",
                                   1,
                                   100,
                                   label="Depth:",
                                   alignment=Qt.AlignRight,
                                   controlWidth=80,
                                   callback=self.settings_changed,
                                   posttext="Recommended range: [1;10]")
        self.learning_rate_spin = gui.doubleSpin(
            box,
            self,
            "learning_rate",
            1e-5,
            1.0,
            1e-5,
            label="Learning rate:",
            decimals=5,
            alignment=Qt.AlignRight,
            controlWidth=80,
            callback=self.settings_changed,
            posttext="Smaller the value, the more iterations")
        self.random_seed_spin = gui.spin(
            box,
            self,
            "random_seed",
            0,
            2**31 - 1,
            controlWidth=80,
            label="Fixed seed for random generator:",
            alignment=Qt.AlignRight,
            callback=self.settings_changed,
            checked="use_random_seed",
            checkCallback=self.settings_changed)

    def create_learner(self):
        if self.base_estimator is None:
            return None
        return self.LEARNER(depth=self.depth,
                            base_estimator=self.base_estimator,
                            iterations=self.n_iterations,
                            learning_rate=self.learning_rate,
                            random_seed=self.random_seed,
                            preprocessors=self.preprocessors,
                            border=self.border)

    @Inputs.learner
    def set_base_learner(self, learner):
        if learner and not learner.supports_weights:
            # Clear the error and reset to default base learner
            self.Error.no_weight_support()
            self.base_estimator = None
            self.base_label.setText("Base estimator: INVALID")
        else:
            self.base_estimator = learner or self.DEFAULT_BASE_ESTIMATOR
            self.base_label.setText("Base estimator: %s" %
                                    self.base_estimator.name.title())
        if self.auto_apply:
            self.apply()